4 # Copyright 2004-2006 Colin Percival
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted providing that the following conditions
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.
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.
30 #### Usage function -- called from command-line handling code.
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
37 usage: `basename $0` [options] command ... [path]
40 -b basedir -- Operate on a system mounted at basedir
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 -s server -- Server from which to fetch updates
48 (default: update.FreeBSD.org)
49 -t address -- Mail output of cron command, if any, to address
52 fetch -- Fetch updates from server
53 cron -- Sleep rand(3600) seconds, fetch updates, and send an
54 email if updates were found
55 install -- Install downloaded updates
56 rollback -- Uninstall most recently installed updates
61 #### Configuration processing functions
64 # Configuration options are set in the following order of priority:
65 # 1. Command line options
66 # 2. Configuration file options
68 # In addition, certain options (e.g., IgnorePaths) can be specified multiple
69 # times and (as long as these are all in the same place, e.g., inside the
70 # configuration file) they will accumulate. Finally, because the path to the
71 # configuration file can be specified at the command line, the entire command
72 # line must be processed before we start reading the configuration file.
74 # Sound like a mess? It is. Here's how we handle this:
75 # 1. Initialize CONFFILE and all the options to "".
76 # 2. Process the command line. Throw an error if a non-accumulating option
78 # 3. If CONFFILE is "", set CONFFILE to /etc/freebsd-update.conf .
79 # 4. For all the configuration options X, set X_saved to X.
80 # 5. Initialize all the options to "".
81 # 6. Read CONFFILE line by line, parsing options.
82 # 7. For each configuration option X, set X to X_saved iff X_saved is not "".
83 # 8. Repeat steps 4-7, except setting options to their default values at (6).
85 CONFIGOPTIONS="KEYPRINT WORKDIR SERVERNAME MAILTO ALLOWADD ALLOWDELETE
86 KEEPMODIFIEDMETADATA COMPONENTS IGNOREPATHS UPDATEIFUNMODIFIED
89 # Set all the configuration options to "".
91 for X in ${CONFIGOPTIONS}; do
96 # For each configuration option X, set X_saved to X.
98 for X in ${CONFIGOPTIONS}; do
99 eval ${X}_saved=\$${X}
103 # For each configuration option X, set X to X_saved if X_saved is not "".
105 for X in ${CONFIGOPTIONS}; do
107 if ! [ -z "${_}" ]; then
108 eval ${X}=\$${X}_saved
113 # Set the trusted keyprint.
115 if [ -z ${KEYPRINT} ]; then
122 # Set the working directory.
124 if [ -z ${WORKDIR} ]; then
131 # Set the name of the server (pool) from which to fetch updates
132 config_ServerName () {
133 if [ -z ${SERVERNAME} ]; then
140 # Set the address to which 'cron' output will be mailed.
142 if [ -z ${MAILTO} ]; then
149 # Set whether FreeBSD Update is allowed to add files (or directories, or
150 # symlinks) which did not previously exist.
152 if [ -z ${ALLOWADD} ]; then
169 # Set whether FreeBSD Update is allowed to remove files/directories/symlinks.
170 config_AllowDelete () {
171 if [ -z ${ALLOWDELETE} ]; then
188 # Set whether FreeBSD Update should keep existing inode ownership,
189 # permissions, and flags, in the event that they have been modified locally
191 config_KeepModifiedMetadata () {
192 if [ -z ${KEEPMODIFIEDMETADATA} ]; then
195 KEEPMODIFIEDMETADATA=yes
198 KEEPMODIFIEDMETADATA=no
209 # Add to the list of components which should be kept updated.
210 config_Components () {
212 COMPONENTS="${COMPONENTS} ${C}"
216 # Add to the list of paths under which updates will be ignored.
217 config_IgnorePaths () {
219 IGNOREPATHS="${IGNOREPATHS} ${C}"
223 # Add to the list of paths within which updates will be performed only if the
224 # file on disk has not been modified locally.
225 config_UpdateIfUnmodified () {
227 UPDATEIFUNMODIFIED="${UPDATEIFUNMODIFIED} ${C}"
231 # Work on a FreeBSD installation mounted under $1
233 if [ -z ${BASEDIR} ]; then
240 # Define what happens to output of utilities
241 config_VerboseLevel () {
242 if [ -z ${VERBOSELEVEL} ]; then
244 [Dd][Ee][Bb][Uu][Gg])
247 [Nn][Oo][Ss][Tt][Aa][Tt][Ss])
250 [Ss][Tt][Aa][Tt][Ss])
262 # Handle one line of configuration
264 if [ $# -eq 0 ]; then
273 #### Parameter handling functions.
275 # Initialize parameters to null, just in case they're
276 # set in the environment.
278 # Configration settings
281 # No configuration file set yet
284 # No commands specified yet
288 # Parse the command line
290 while [ $# -gt 0 ]; do
292 # Location of configuration file
294 if [ $# -eq 1 ]; then usage; fi
295 if [ ! -z "${CONFFILE}" ]; then usage; fi
299 # Configuration file equivalents
301 if [ $# -eq 1 ]; then usage; fi; shift
302 config_BaseDir $1 || usage
305 if [ $# -eq 1 ]; then usage; fi; shift
306 config_WorkDir $1 || usage
309 if [ $# -eq 1 ]; then usage; fi; shift
310 config_KeyPrint $1 || usage
313 if [ $# -eq 1 ]; then usage; fi; shift
314 config_ServerName $1 || usage
317 if [ $# -eq 1 ]; then usage; fi; shift
318 config_MailTo $1 || usage
321 if [ $# -eq 1 ]; then usage; fi; shift
322 config_VerboseLevel $1 || usage
325 # Aliases for "-v debug" and "-v nostats"
327 config_VerboseLevel debug || usage
330 config_VerboseLevel nostats || usage
334 cron | fetch | install | rollback)
335 COMMANDS="${COMMANDS} $1"
338 # Anything else is an error
346 # Make sure we have at least one command
347 if [ -z "${COMMANDS}" ]; then
352 # Parse the configuration file
354 # If a configuration file was specified on the command line, check
355 # that it exists and is readable.
356 if [ ! -z "${CONFFILE}" ] && [ ! -r "${CONFFILE}" ]; then
357 echo -n "File does not exist "
358 echo -n "or is not readable: "
363 # If a configuration file was not specified on the command line,
364 # use the default configuration file path. If that default does
365 # not exist, give up looking for any configuration.
366 if [ -z "${CONFFILE}" ]; then
367 CONFFILE="/etc/freebsd-update.conf"
368 if [ ! -r "${CONFFILE}" ]; then
373 # Save the configuration options specified on the command line, and
374 # clear all the options in preparation for reading the config file.
378 # Read the configuration file. Anything after the first '#' is
379 # ignored, and any blank lines are ignored.
383 LINEX=`echo "${LINE}" | cut -f 1 -d '#'`
384 if ! configline ${LINEX}; then
385 echo "Error processing configuration file, line $L:"
391 # Merge the settings read from the configuration file with those
392 # provided at the command line.
396 # Provide some default parameters
398 # Save any parameters already configured, and clear the slate
402 # Default configurations
403 config_WorkDir /var/db/freebsd-update
406 config_AllowDelete yes
407 config_KeepModifiedMetadata yes
409 config_VerboseLevel stats
411 # Merge these defaults into the earlier-configured settings
415 # Set utility output filtering options, based on ${VERBOSELEVEL}
416 fetch_setup_verboselevel () {
417 case ${VERBOSELEVEL} in
419 QUIETREDIR="/dev/stderr"
421 STATSREDIR="/dev/stderr"
429 STATSREDIR="/dev/null"
435 QUIETREDIR="/dev/null"
437 STATSREDIR="/dev/stdout"
445 # Perform sanity checks and set some final parameters
446 # in preparation for fetching files. Figure out which
447 # set of updates should be downloaded: If the user is
448 # running *-p[0-9]+, strip off the last part; if the
449 # user is running -SECURITY, call it -RELEASE. Chdir
450 # into the working directory.
451 fetch_check_params () {
452 export HTTP_USER_AGENT="freebsd-update (${COMMAND}, `uname -r`)"
455 "SERVERNAME must be given via command line or configuration file."
456 _KEYPRINT_z="Key must be given via -k option or configuration file."
457 _KEYPRINT_bad="Invalid key fingerprint: "
458 _WORKDIR_bad="Directory does not exist or is not writable: "
460 if [ -z "${SERVERNAME}" ]; then
461 echo -n "`basename $0`: "
462 echo "${_SERVERNAME_z}"
465 if [ -z "${KEYPRINT}" ]; then
466 echo -n "`basename $0`: "
467 echo "${_KEYPRINT_z}"
470 if ! echo "${KEYPRINT}" | grep -qE "^[0-9a-f]{64}$"; then
471 echo -n "`basename $0`: "
472 echo -n "${_KEYPRINT_bad}"
476 if ! [ -d "${WORKDIR}" -a -w "${WORKDIR}" ]; then
477 echo -n "`basename $0`: "
478 echo -n "${_WORKDIR_bad}"
482 cd ${WORKDIR} || exit 1
484 # Generate release number. The s/SECURITY/RELEASE/ bit exists
485 # to provide an upgrade path for FreeBSD Update 1.x users, since
486 # the kernels provided by FreeBSD Update 1.x are always labelled
489 sed -E 's,-p[0-9]+,,' |
490 sed -E 's,-SECURITY,-RELEASE,'`
492 FETCHDIR=${RELNUM}/${ARCH}
494 # Figure out what directory contains the running kernel
495 BOOTFILE=`sysctl -n kern.bootfile`
496 KERNELDIR=${BOOTFILE%/kernel}
497 if ! [ -d ${KERNELDIR} ]; then
498 echo "Cannot identify running kernel"
503 BSPATCH=/usr/bin/bspatch
505 PHTTPGET=/usr/libexec/phttpget
507 # Set up variables relating to VERBOSELEVEL
508 fetch_setup_verboselevel
510 # Construct a unique name from ${BASEDIR}
511 BDHASH=`echo ${BASEDIR} | sha256 -q`
514 # Perform sanity checks and set some final parameters in
515 # preparation for installing updates.
516 install_check_params () {
517 # Check that we are root. All sorts of things won't work otherwise.
518 if [ `id -u` != 0 ]; then
519 echo "You must be root to run this."
523 # Check that we have a working directory
524 _WORKDIR_bad="Directory does not exist or is not writable: "
525 if ! [ -d "${WORKDIR}" -a -w "${WORKDIR}" ]; then
526 echo -n "`basename $0`: "
527 echo -n "${_WORKDIR_bad}"
531 cd ${WORKDIR} || exit 1
533 # Construct a unique name from ${BASEDIR}
534 BDHASH=`echo ${BASEDIR} | sha256 -q`
536 # Check that we have updates ready to install
537 if ! [ -L ${BDHASH}-install ]; then
538 echo "No updates are available to install."
539 echo "Run '$0 fetch' first."
542 if ! [ -f ${BDHASH}-install/INDEX-OLD ] ||
543 ! [ -f ${BDHASH}-install/INDEX-NEW ]; then
544 echo "Update manifest is corrupt -- this should never happen."
545 echo "Re-run '$0 fetch'."
550 # Perform sanity checks and set some final parameters in
551 # preparation for UNinstalling updates.
552 rollback_check_params () {
553 # Check that we are root. All sorts of things won't work otherwise.
554 if [ `id -u` != 0 ]; then
555 echo "You must be root to run this."
559 # Check that we have a working directory
560 _WORKDIR_bad="Directory does not exist or is not writable: "
561 if ! [ -d "${WORKDIR}" -a -w "${WORKDIR}" ]; then
562 echo -n "`basename $0`: "
563 echo -n "${_WORKDIR_bad}"
567 cd ${WORKDIR} || exit 1
569 # Construct a unique name from ${BASEDIR}
570 BDHASH=`echo ${BASEDIR} | sha256 -q`
572 # Check that we have updates ready to rollback
573 if ! [ -L ${BDHASH}-rollback ]; then
574 echo "No rollback directory found."
577 if ! [ -f ${BDHASH}-rollback/INDEX-OLD ] ||
578 ! [ -f ${BDHASH}-rollback/INDEX-NEW ]; then
579 echo "Update manifest is corrupt -- this should never happen."
584 #### Core functionality -- the actual work gets done here
586 # Use an SRV query to pick a server. If the SRV query doesn't provide
587 # a useful answer, use the server name specified by the user.
588 # Put another way... look up _http._tcp.${SERVERNAME} and pick a server
589 # from that; or if no servers are returned, use ${SERVERNAME}.
590 # This allows a user to specify "portsnap.freebsd.org" (in which case
591 # portsnap will select one of the mirrors) or "portsnap5.tld.freebsd.org"
592 # (in which case portsnap will use that particular server, since there
593 # won't be an SRV entry for that name).
595 # We ignore the Port field, since we are always going to use port 80.
597 # Fetch the mirror list, but do not pick a mirror yet. Returns 1 if
598 # no mirrors are available for any reason.
599 fetch_pick_server_init () {
602 # Check that host(1) exists (i.e., that the system wasn't built with the
603 # WITHOUT_BIND set) and don't try to find a mirror if it doesn't exist.
604 if ! which -s host; then
609 echo -n "Looking up ${SERVERNAME} mirrors... "
611 # Issue the SRV query and pull out the Priority, Weight, and Target fields.
612 # BIND 9 prints "$name has SRV record ..." while BIND 8 prints
613 # "$name server selection ..."; we allow either format.
614 MLIST="_http._tcp.${SERVERNAME}"
615 host -t srv "${MLIST}" |
616 sed -nE "s/${MLIST} (has SRV record|server selection) //p" |
617 cut -f 1,2,4 -d ' ' |
619 sort > serverlist_full
621 # If no records, give up -- we'll just use the server name we were given.
622 if [ `wc -l < serverlist_full` -eq 0 ]; then
627 # Report how many mirrors we found.
628 echo `wc -l < serverlist_full` "mirrors found."
630 # Generate a random seed for use in picking mirrors. If HTTP_PROXY
631 # is set, this will be used to generate the seed; otherwise, the seed
633 if [ -n "${HTTP_PROXY}${http_proxy}" ]; then
634 RANDVALUE=`sha256 -qs "${HTTP_PROXY}${http_proxy}" |
638 RANDVALUE=`jot -r 1 0 999999999`
642 # Pick a mirror. Returns 1 if we have run out of mirrors to try.
643 fetch_pick_server () {
644 # Generate a list of not-yet-tried mirrors
645 sort serverlist_tried |
646 comm -23 serverlist_full - > serverlist
648 # Have we run out of mirrors?
649 if [ `wc -l < serverlist` -eq 0 ]; then
650 echo "No mirrors remaining, giving up."
654 # Find the highest priority level (lowest numeric value).
655 SRV_PRIORITY=`cut -f 1 -d ' ' serverlist | sort -n | head -1`
657 # Add up the weights of the response lines at that priority level.
662 SRV_W=`echo $X | cut -f 2 -d ' '`
663 SRV_WSUM=$(($SRV_WSUM + $SRV_W))
668 # If all the weights are 0, pretend that they are all 1 instead.
669 if [ ${SRV_WSUM} -eq 0 ]; then
670 SRV_WSUM=`grep -E "^${SRV_PRIORITY} " serverlist | wc -l`
676 # Pick a value between 0 and the sum of the weights - 1
677 SRV_RND=`expr ${RANDVALUE} % ${SRV_WSUM}`
679 # Read through the list of mirrors and set SERVERNAME. Write the line
680 # corresponding to the mirror we selected into serverlist_tried so that
681 # we won't try it again.
685 SRV_W=`echo $X | cut -f 2 -d ' '`
686 SRV_W=$(($SRV_W + $SRV_W_ADD))
687 if [ $SRV_RND -lt $SRV_W ]; then
688 SERVERNAME=`echo $X | cut -f 3 -d ' '`
689 echo "$X" >> serverlist_tried
692 SRV_RND=$(($SRV_RND - $SRV_W))
699 # Take a list of ${oldhash}|${newhash} and output a list of needed patches,
700 # i.e., those for which we have ${oldhash} and don't have ${newhash}.
701 fetch_make_patchlist () {
702 grep -vE "^([0-9a-f]{64})\|\1$" |
705 if [ -f "files/${Y}.gz" ] ||
706 [ ! -f "files/${X}.gz" ]; then
713 # Print user-friendly progress statistics
718 if [ $(($LNC % 10)) = 0 ]; then
720 elif [ $(($LNC % 2)) = 0 ]; then
727 # Initialize the working directory
733 # Check that we have a public key with an appropriate hash, or
734 # fetch the key if it doesn't exist. Returns 1 if the key has
735 # not yet been fetched.
737 if [ -r pub.ssl ] && [ `${SHA256} -q pub.ssl` = ${KEYPRINT} ]; then
741 echo -n "Fetching public key from ${SERVERNAME}... "
743 fetch ${QUIETFLAG} http://${SERVERNAME}/${FETCHDIR}/pub.ssl \
744 2>${QUIETREDIR} || true
745 if ! [ -r pub.ssl ]; then
749 if ! [ `${SHA256} -q pub.ssl` = ${KEYPRINT} ]; then
750 echo "key has incorrect hash."
757 # Fetch metadata signature, aka "tag".
759 echo ${NDEBUG} "Fetching metadata signature from ${SERVERNAME}... "
761 fetch ${QUIETFLAG} http://${SERVERNAME}/${FETCHDIR}/latest.ssl \
762 2>${QUIETREDIR} || true
763 if ! [ -r latest.ssl ]; then
768 openssl rsautl -pubin -inkey pub.ssl -verify \
769 < latest.ssl > tag.new 2>${QUIETREDIR} || true
772 if ! [ `wc -l < tag.new` = 1 ] ||
774 "^freebsd-update\|${ARCH}\|${RELNUM}\|[0-9]+\|[0-9a-f]{64}\|[0-9]{10}" \
776 echo "invalid signature."
782 RELPATCHNUM=`cut -f 4 -d '|' < tag.new`
783 TINDEXHASH=`cut -f 5 -d '|' < tag.new`
784 EOLTIME=`cut -f 6 -d '|' < tag.new`
787 # Sanity-check the patch number in a tag, to make sure that we're not
788 # going to "update" backwards and to prevent replay attacks.
790 # Check that we're not going to move from -pX to -pY with Y < X.
791 RELPX=`uname -r | sed -E 's,.*-,,'`
792 if echo ${RELPX} | grep -qE '^p[0-9]+$'; then
793 RELPX=`echo ${RELPX} | cut -c 2-`
797 if [ "${RELPATCHNUM}" -lt "${RELPX}" ]; then
799 echo -n "Files on mirror (${RELNUM}-p${RELPATCHNUM})"
800 echo " appear older than what"
801 echo "we are currently running (`uname -r`)!"
802 echo "Cowardly refusing to proceed any further."
806 # If "tag" exists and corresponds to ${RELNUM}, make sure that
807 # it contains a patch number <= RELPATCHNUM, in order to protect
808 # against rollback (replay) attacks.
811 "^freebsd-update\|${ARCH}\|${RELNUM}\|[0-9]+\|[0-9a-f]{64}\|[0-9]{10}" \
813 LASTRELPATCHNUM=`cut -f 4 -d '|' < tag`
815 if [ "${RELPATCHNUM}" -lt "${LASTRELPATCHNUM}" ]; then
817 echo -n "Files on mirror (${RELNUM}-p${RELPATCHNUM})"
818 echo " are older than the"
819 echo -n "most recently seen updates"
820 echo " (${RELNUM}-p${LASTRELPATCHNUM})."
821 echo "Cowardly refusing to proceed any further."
827 # Fetch metadata index file
828 fetch_metadata_index () {
829 echo ${NDEBUG} "Fetching metadata index... "
831 fetch ${QUIETFLAG} http://${SERVERNAME}/${FETCHDIR}/t/${TINDEXHASH}
833 if ! [ -f ${TINDEXHASH} ]; then
837 if [ `${SHA256} -q ${TINDEXHASH}` != ${TINDEXHASH} ]; then
838 echo "update metadata index corrupt."
844 # Print an error message about signed metadata being bogus.
845 fetch_metadata_bogus () {
847 echo "The update metadata$1 is correctly signed, but"
848 echo "failed an integrity check."
849 echo "Cowardly refusing to proceed any further."
853 # Construct tINDEX.new by merging the lines named in $1 from ${TINDEXHASH}
854 # with the lines not named in $@ from tINDEX.present (if that file exists).
855 fetch_metadata_index_merge () {
856 for METAFILE in $@; do
857 if [ `grep -E "^${METAFILE}\|" ${TINDEXHASH} | wc -l` \
859 fetch_metadata_bogus " index"
863 grep -E "${METAFILE}\|" ${TINDEXHASH}
867 if [ -f tINDEX.present ]; then
868 join -t '|' -v 2 tINDEX.wanted tINDEX.present |
869 sort -m - tINDEX.wanted > tINDEX.new
872 mv tINDEX.wanted tINDEX.new
876 # Sanity check all the lines of tINDEX.new. Even if more metadata lines
877 # are added by future versions of the server, this won't cause problems,
878 # since the only lines which appear in tINDEX.new are the ones which we
879 # specifically grepped out of ${TINDEXHASH}.
880 fetch_metadata_index_sanity () {
881 if grep -qvE '^[0-9A-Z.-]+\|[0-9a-f]{64}$' tINDEX.new; then
882 fetch_metadata_bogus " index"
887 # Sanity check the metadata file $1.
888 fetch_metadata_sanity () {
889 # Some aliases to save space later: ${P} is a character which can
890 # appear in a path; ${M} is the four numeric metadata fields; and
891 # ${H} is a sha256 hash.
892 P="[-+./:=_[[:alnum:]]"
893 M="[0-9]+\|[0-9]+\|[0-9]+\|[0-9]+"
896 # Check that the first four fields make sense.
897 if gunzip -c < files/$1.gz |
898 grep -qvE "^[a-z]+\|[0-9a-z]+\|${P}+\|[fdL-]\|"; then
899 fetch_metadata_bogus ""
903 # Remove the first three fields.
904 gunzip -c < files/$1.gz |
905 cut -f 4- -d '|' > sanitycheck.tmp
907 # Sanity check entries with type 'f'
908 if grep -E '^f' sanitycheck.tmp |
909 grep -qvE "^f\|${M}\|${H}\|${P}*\$"; then
910 fetch_metadata_bogus ""
914 # Sanity check entries with type 'd'
915 if grep -E '^d' sanitycheck.tmp |
916 grep -qvE "^d\|${M}\|\|\$"; then
917 fetch_metadata_bogus ""
921 # Sanity check entries with type 'L'
922 if grep -E '^L' sanitycheck.tmp |
923 grep -qvE "^L\|${M}\|${P}*\|\$"; then
924 fetch_metadata_bogus ""
928 # Sanity check entries with type '-'
929 if grep -E '^-' sanitycheck.tmp |
930 grep -qvE "^-\|\|\|\|\|\|"; then
931 fetch_metadata_bogus ""
939 # Fetch the metadata index and metadata files listed in $@,
940 # taking advantage of metadata patches where possible.
942 fetch_metadata_index || return 1
943 fetch_metadata_index_merge $@ || return 1
944 fetch_metadata_index_sanity || return 1
946 # Generate a list of wanted metadata patches
947 join -t '|' -o 1.2,2.2 tINDEX.present tINDEX.new |
948 fetch_make_patchlist > patchlist
950 if [ -s patchlist ]; then
951 # Attempt to fetch metadata patches
952 echo -n "Fetching `wc -l < patchlist | tr -d ' '` "
953 echo ${NDEBUG} "metadata patches.${DDSTATS}"
954 tr '|' '-' < patchlist |
955 lam -s "${FETCHDIR}/tp/" - -s ".gz" |
956 xargs ${XARGST} ${PHTTPGET} ${SERVERNAME} \
957 2>${STATSREDIR} | fetch_progress
960 # Attempt to apply metadata patches
961 echo -n "Applying metadata patches... "
962 tr '|' ' ' < patchlist |
964 if [ ! -f "${X}-${Y}.gz" ]; then continue; fi
965 gunzip -c < ${X}-${Y}.gz > diff
966 gunzip -c < files/${X}.gz > diff-OLD
968 # Figure out which lines are being added and removed
971 while read PREFIX; do
972 look "${PREFIX}" diff-OLD
978 # Generate the new file
979 comm -23 diff-OLD diff-rm |
980 sort - diff-add > diff-NEW
982 if [ `${SHA256} -q diff-NEW` = ${Y} ]; then
983 mv diff-NEW files/${Y}
988 rm -f ${X}-${Y}.gz diff
989 rm -f diff-OLD diff-NEW diff-add diff-rm
994 # Update metadata without patches
995 cut -f 2 -d '|' < tINDEX.new |
997 if [ ! -f "files/${Y}.gz" ]; then
1002 if [ -s filelist ]; then
1003 echo -n "Fetching `wc -l < filelist | tr -d ' '` "
1004 echo ${NDEBUG} "metadata files... "
1005 lam -s "${FETCHDIR}/m/" - -s ".gz" < filelist |
1006 xargs ${XARGST} ${PHTTPGET} ${SERVERNAME} \
1010 if ! [ -f ${Y}.gz ]; then
1014 if [ `gunzip -c < ${Y}.gz |
1015 ${SHA256} -q` = ${Y} ]; then
1016 mv ${Y}.gz files/${Y}.gz
1018 echo "metadata is corrupt."
1025 # Sanity-check the metadata files.
1026 cut -f 2 -d '|' tINDEX.new > filelist
1028 fetch_metadata_sanity ${X} || return 1
1031 # Remove files which are no longer needed
1032 cut -f 2 -d '|' tINDEX.present |
1034 cut -f 2 -d '|' tINDEX.new |
1036 comm -13 - oldfiles |
1037 lam -s "files/" - -s ".gz" |
1039 rm patchlist filelist oldfiles
1043 mv tINDEX.new tINDEX.present
1049 # Generated a filtered version of the metadata file $1 from the downloaded
1050 # file, by fishing out the lines corresponding to components we're trying
1051 # to keep updated, and then removing lines corresponding to paths we want
1053 fetch_filter_metadata () {
1054 METAHASH=`look "$1|" tINDEX.present | cut -f 2 -d '|'`
1055 gunzip -c < files/${METAHASH}.gz > $1.all
1057 # Fish out the lines belonging to components we care about.
1058 # Canonicalize directory names by removing any trailing / in
1059 # order to avoid listing directories multiple times if they
1060 # belong to multiple components. Turning "/" into "" doesn't
1061 # matter, since we add a leading "/" when we use paths later.
1062 for C in ${COMPONENTS}; do
1063 look "`echo ${C} | tr '/' '|'`|" $1.all
1066 sed -e 's,/|d|,|d|,' |
1069 # Figure out which lines to ignore and remove them.
1070 for X in ${IGNOREPATHS}; do
1071 grep -E "^${X}" $1.tmp
1074 comm -13 - $1.tmp > $1
1076 # Remove temporary files.
1080 # Filter the metadata file $1 by adding lines with
1084 # (or more generally, `sysctl -n kern.bootfile` minus the trailing "/kernel").
1085 fetch_filter_kernel_names () {
1086 grep ^/boot/`uname -i` $1 |
1087 sed -e "s,/boot/`uname -i`,${KERNELDIR}," |
1092 # For all paths appearing in $1 or $3, inspect the system
1093 # and generate $2 describing what is currently installed.
1094 fetch_inspect_system () {
1098 # Tell the user why his disk is suddenly making lots of noise
1099 echo -n "Inspecting system... "
1101 # Generate list of files to inspect
1106 # Examine each file and output lines of the form
1107 # /path/to/file|type|device-inum|user|group|perm|flags|value
1108 # sorted by device and inode number.
1110 # If the symlink/file/directory does not exist, record this.
1111 if ! [ -e ${BASEDIR}/${F} ]; then
1115 if ! [ -r ${BASEDIR}/${F} ]; then
1116 echo "Cannot read file: ${BASEDIR}/${F}" \
1122 # Otherwise, output an index line.
1123 if [ -L ${BASEDIR}/${F} ]; then
1125 stat -n -f '%d-%i|%u|%g|%Mp%Lp|%Of|' ${BASEDIR}/${F};
1126 readlink ${BASEDIR}/${F};
1127 elif [ -f ${BASEDIR}/${F} ]; then
1129 stat -n -f '%d-%i|%u|%g|%Mp%Lp|%Of|' ${BASEDIR}/${F};
1130 sha256 -q ${BASEDIR}/${F};
1131 elif [ -d ${BASEDIR}/${F} ]; then
1133 stat -f '%d-%i|%u|%g|%Mp%Lp|%Of|' ${BASEDIR}/${F};
1135 echo "Unknown file type: ${BASEDIR}/${F}" \
1141 sort -k 3,3 -t '|' > $2.tmp
1144 # Check if an error occured during system inspection
1145 if [ -f .err ]; then
1149 # Convert to the form
1150 # /path/to/file|type|user|group|perm|flags|value|hlink
1151 # by resolving identical device and inode numbers into hard links.
1152 cut -f 1,3 -d '|' $2.tmp |
1153 sort -k 1,1 -t '|' |
1154 sort -s -u -k 2,2 -t '|' |
1155 join -1 2 -2 3 -t '|' - $2.tmp |
1156 awk -F \| -v OFS=\| \
1158 if (($2 == $3) || ($4 == "-"))
1159 print $3,$4,$5,$6,$7,$8,$9,""
1161 print $3,$4,$5,$6,$7,$8,$9,$2
1166 # We're finished looking around
1170 # For any paths matching ${UPDATEIFUNMODIFIED}, remove lines from $[123]
1171 # which correspond to lines in $2 with hashes not matching $1 or $3. For
1172 # entries in $2 marked "not present" (aka. type -), remove lines from $[123]
1173 # unless there is a corresponding entry in $1.
1174 fetch_filter_unmodified_notpresent () {
1175 # Figure out which lines of $1 and $3 correspond to bits which
1176 # should only be updated if they haven't changed, and fish out
1177 # the (path, type, value) tuples.
1178 # NOTE: We don't consider a file to be "modified" if it matches
1180 for X in ${UPDATEIFUNMODIFIED}; do
1184 cut -f 1,2,7 -d '|' |
1187 # Do the same for $2.
1188 for X in ${UPDATEIFUNMODIFIED}; do
1191 cut -f 1,2,7 -d '|' |
1194 # Any entry in $2-values which is not in $1-values corresponds to
1195 # a path which we need to remove from $1, $2, and $3.
1196 comm -13 $1-values $2-values > mlines
1197 rm $1-values $2-values
1199 # Any lines in $2 which are not in $1 AND are "not present" lines
1200 # also belong in mlines.
1202 cut -f 1,2,7 -d '|' |
1203 fgrep '|-|' >> mlines
1205 # Remove lines from $1, $2, and $3
1206 for X in $1 $2 $3; do
1207 sort -t '|' -k 1,1 ${X} > ${X}.tmp
1208 cut -f 1 -d '|' < mlines |
1210 join -v 2 -t '|' - ${X}.tmp |
1215 # Store a list of the modified files, for future reference
1216 fgrep -v '|-|' mlines |
1217 cut -f 1 -d '|' > modifiedfiles
1221 # For each entry in $1 of type -, remove any corresponding
1222 # entry from $2 if ${ALLOWADD} != "yes". Remove all entries
1223 # of type - from $1.
1224 fetch_filter_allowadd () {
1225 cut -f 1,2 -d '|' < $1 |
1227 cut -f 1 -d '|' > filesnotpresent
1229 if [ ${ALLOWADD} != "yes" ]; then
1231 join -v 1 -t '|' - filesnotpresent |
1237 join -v 1 -t '|' - filesnotpresent |
1243 # If ${ALLOWDELETE} != "yes", then remove any entries from $1
1244 # which don't correspond to entries in $2.
1245 fetch_filter_allowdelete () {
1246 # Produce a lists ${PATH}|${TYPE}
1248 cut -f 1-2 -d '|' < ${X} |
1249 sort -u > ${X}.nodes
1252 # Figure out which lines need to be removed from $1.
1253 if [ ${ALLOWDELETE} != "yes" ]; then
1254 comm -23 $1.nodes $2.nodes > $1.badnodes
1259 # Remove the relevant lines from $1
1262 done < $1.badnodes |
1263 comm -13 - $1 > $1.tmp
1266 rm $1.badnodes $1.nodes $2.nodes
1269 # If ${KEEPMODIFIEDMETADATA} == "yes", then for each entry in $2
1270 # with metadata not matching any entry in $1, replace the corresponding
1271 # line of $3 with one having the same metadata as the entry in $2.
1272 fetch_filter_modified_metadata () {
1273 # Fish out the metadata from $1 and $2
1275 cut -f 1-6 -d '|' < ${X} > ${X}.metadata
1278 # Find the metadata we need to keep
1279 if [ ${KEEPMODIFIEDMETADATA} = "yes" ]; then
1280 comm -13 $1.metadata $2.metadata > keepmeta
1285 # Extract the lines which we need to remove from $3, and
1286 # construct the lines which we need to add to $3.
1290 NODE=`echo "${LINE}" | cut -f 1-2 -d '|'`
1291 look "${NODE}|" $3 >> $3.remove
1292 look "${NODE}|" $3 |
1294 lam -s "${LINE}|" - >> $3.add
1297 # Remove the specified lines and add the new lines.
1300 sort -u - $3.add > $3.tmp
1303 rm keepmeta $1.metadata $2.metadata $3.add $3.remove
1306 # Remove lines from $1 and $2 which are identical;
1307 # no need to update a file if it isn't changing.
1308 fetch_filter_uptodate () {
1309 comm -23 $1 $2 > $1.tmp
1310 comm -13 $1 $2 > $2.tmp
1316 # Prepare to fetch files: Generate a list of the files we need,
1317 # copy the unmodified files we have into /files/, and generate
1318 # a list of patches to download.
1319 fetch_files_prepare () {
1320 # Tell the user why his disk is suddenly making lots of noise
1321 echo -n "Preparing to download files... "
1323 # Reduce indices to ${PATH}|${HASH} pairs
1324 for X in $1 $2 $3; do
1325 cut -f 1,2,7 -d '|' < ${X} |
1331 # List of files wanted
1332 cut -f 2 -d '|' < $3.hashes |
1333 sort -u > files.wanted
1335 # Generate a list of unmodified files
1336 comm -12 $1.hashes $2.hashes |
1337 sort -k 1,1 -t '|' > unmodified.files
1339 # Copy all files into /files/. We only need the unmodified files
1340 # for use in patching; but we'll want all of them if the user asks
1341 # to rollback the updates later.
1342 cut -f 1 -d '|' < $2.hashes |
1344 cp "${BASEDIR}/${F}" tmpfile
1345 gzip -c < tmpfile > files/`sha256 -q tmpfile`.gz
1349 # Produce a list of patches to download
1350 sort -k 1,1 -t '|' $3.hashes |
1351 join -t '|' -o 2.2,1.2 - unmodified.files |
1352 fetch_make_patchlist > patchlist
1355 rm unmodified.files $1.hashes $2.hashes $3.hashes
1357 # We don't need the list of possible old files any more.
1360 # We're finished making noise
1366 # Attempt to fetch patches
1367 if [ -s patchlist ]; then
1368 echo -n "Fetching `wc -l < patchlist | tr -d ' '` "
1369 echo ${NDEBUG} "patches.${DDSTATS}"
1370 tr '|' '-' < patchlist |
1371 lam -s "${FETCHDIR}/bp/" - |
1372 xargs ${XARGST} ${PHTTPGET} ${SERVERNAME} \
1373 2>${STATSREDIR} | fetch_progress
1376 # Attempt to apply patches
1377 echo -n "Applying patches... "
1378 tr '|' ' ' < patchlist |
1380 if [ ! -f "${X}-${Y}" ]; then continue; fi
1381 gunzip -c < files/${X}.gz > OLD
1383 bspatch OLD NEW ${X}-${Y}
1385 if [ `${SHA256} -q NEW` = ${Y} ]; then
1389 rm -f diff OLD NEW ${X}-${Y}
1390 done 2>${QUIETREDIR}
1394 # Download files which couldn't be generate via patching
1396 if [ ! -f "files/${Y}.gz" ]; then
1399 done < files.wanted > filelist
1401 if [ -s filelist ]; then
1402 echo -n "Fetching `wc -l < filelist | tr -d ' '` "
1403 echo ${NDEBUG} "files... "
1404 lam -s "${FETCHDIR}/f/" - -s ".gz" < filelist |
1405 xargs ${XARGST} ${PHTTPGET} ${SERVERNAME} \
1409 if ! [ -f ${Y}.gz ]; then
1413 if [ `gunzip -c < ${Y}.gz |
1414 ${SHA256} -q` = ${Y} ]; then
1415 mv ${Y}.gz files/${Y}.gz
1417 echo "${Y} has incorrect hash."
1425 rm files.wanted filelist patchlist
1428 # Create and populate install manifest directory; and report what updates
1430 fetch_create_manifest () {
1431 # If we have an existing install manifest, nuke it.
1432 if [ -L "${BDHASH}-install" ]; then
1433 rm -r ${BDHASH}-install/
1434 rm ${BDHASH}-install
1437 # Report to the user if any updates were avoided due to local changes
1438 if [ -s modifiedfiles ]; then
1440 echo -n "The following files are affected by updates, "
1441 echo "but no changes have"
1442 echo -n "been downloaded because the files have been "
1443 echo "modified locally:"
1448 # If no files will be updated, tell the user and exit
1449 if ! [ -s INDEX-PRESENT ] &&
1450 ! [ -s INDEX-NEW ]; then
1451 rm INDEX-PRESENT INDEX-NEW
1453 echo -n "No updates needed to update system to "
1454 echo "${RELNUM}-p${RELPATCHNUM}."
1458 # Divide files into (a) removed files, (b) added files, and
1459 # (c) updated files.
1460 cut -f 1 -d '|' < INDEX-PRESENT |
1461 sort > INDEX-PRESENT.flist
1462 cut -f 1 -d '|' < INDEX-NEW |
1463 sort > INDEX-NEW.flist
1464 comm -23 INDEX-PRESENT.flist INDEX-NEW.flist > files.removed
1465 comm -13 INDEX-PRESENT.flist INDEX-NEW.flist > files.added
1466 comm -12 INDEX-PRESENT.flist INDEX-NEW.flist > files.updated
1467 rm INDEX-PRESENT.flist INDEX-NEW.flist
1469 # Report removed files, if any
1470 if [ -s files.removed ]; then
1472 echo -n "The following files will be removed "
1473 echo "as part of updating to ${RELNUM}-p${RELPATCHNUM}:"
1478 # Report added files, if any
1479 if [ -s files.added ]; then
1481 echo -n "The following files will be added "
1482 echo "as part of updating to ${RELNUM}-p${RELPATCHNUM}:"
1487 # Report updated files, if any
1488 if [ -s files.updated ]; then
1490 echo -n "The following files will be updated "
1491 echo "as part of updating to ${RELNUM}-p${RELPATCHNUM}:"
1497 # Create a directory for the install manifest.
1498 MDIR=`mktemp -d install.XXXXXX` || return 1
1501 mv INDEX-PRESENT ${MDIR}/INDEX-OLD
1502 mv INDEX-NEW ${MDIR}/INDEX-NEW
1504 # Link it into place
1505 ln -s ${MDIR} ${BDHASH}-install
1508 # Warn about any upcoming EoL
1510 # What's the current time?
1511 NOWTIME=`date "+%s"`
1513 # When did we last warn about the EoL date?
1514 if [ -f lasteolwarn ]; then
1515 LASTWARN=`cat lasteolwarn`
1517 LASTWARN=`expr ${NOWTIME} - 63072000`
1520 # If the EoL time is past, warn.
1521 if [ ${EOLTIME} -lt ${NOWTIME} ]; then
1524 WARNING: `uname -sr` HAS PAST ITS END-OF-LIFE DATE.
1525 Any security issues discovered after `date -r ${EOLTIME}`
1526 will not have been corrected.
1531 # Figure out how long it has been since we last warned about the
1532 # upcoming EoL, and how much longer we have left.
1533 SINCEWARN=`expr ${NOWTIME} - ${LASTWARN}`
1534 TIMELEFT=`expr ${EOLTIME} - ${NOWTIME}`
1536 # Don't warn if the EoL is more than 6 months away
1537 if [ ${TIMELEFT} -gt 15768000 ]; then
1541 # Don't warn if the time remaining is more than 3 times the time
1542 # since the last warning.
1543 if [ ${TIMELEFT} -gt `expr ${SINCEWARN} \* 3` ]; then
1547 # Figure out what time units to use.
1548 if [ ${TIMELEFT} -lt 604800 ]; then
1551 elif [ ${TIMELEFT} -lt 2678400 ]; then
1559 # Compute the right number of units
1560 NUM=`expr ${TIMELEFT} / ${SIZE}`
1561 if [ ${NUM} != 1 ]; then
1568 WARNING: `uname -sr` is approaching its End-of-Life date.
1569 It is strongly recommended that you upgrade to a newer
1570 release within the next ${NUM} ${UNIT}.
1573 # Update the stored time of last warning
1574 echo ${NOWTIME} > lasteolwarn
1577 # Do the actual work involved in "fetch" / "cron".
1579 workdir_init || return 1
1581 # Prepare the mirror list.
1582 fetch_pick_server_init && fetch_pick_server
1584 # Try to fetch the public key until we run out of servers.
1585 while ! fetch_key; do
1586 fetch_pick_server || return 1
1589 # Try to fetch the metadata index signature ("tag") until we run
1590 # out of available servers; and sanity check the downloaded tag.
1591 while ! fetch_tag; do
1592 fetch_pick_server || return 1
1594 fetch_tagsanity || return 1
1596 # Fetch the latest INDEX-NEW and INDEX-OLD files.
1597 fetch_metadata INDEX-NEW INDEX-OLD || return 1
1599 # Generate filtered INDEX-NEW and INDEX-OLD files containing only
1600 # the lines which (a) belong to components we care about, and (b)
1601 # don't correspond to paths we're explicitly ignoring.
1602 fetch_filter_metadata INDEX-NEW || return 1
1603 fetch_filter_metadata INDEX-OLD || return 1
1605 # Translate /boot/`uname -i` into ${KERNELDIR}
1606 fetch_filter_kernel_names INDEX-NEW
1607 fetch_filter_kernel_names INDEX-OLD
1609 # For all paths appearing in INDEX-OLD or INDEX-NEW, inspect the
1610 # system and generate an INDEX-PRESENT file.
1611 fetch_inspect_system INDEX-OLD INDEX-PRESENT INDEX-NEW || return 1
1613 # Based on ${UPDATEIFUNMODIFIED}, remove lines from INDEX-* which
1614 # correspond to lines in INDEX-PRESENT with hashes not appearing
1615 # in INDEX-OLD or INDEX-NEW. Also remove lines where the entry in
1616 # INDEX-PRESENT has type - and there isn't a corresponding entry in
1617 # INDEX-OLD with type -.
1618 fetch_filter_unmodified_notpresent INDEX-OLD INDEX-PRESENT INDEX-NEW
1620 # For each entry in INDEX-PRESENT of type -, remove any corresponding
1621 # entry from INDEX-NEW if ${ALLOWADD} != "yes". Remove all entries
1622 # of type - from INDEX-PRESENT.
1623 fetch_filter_allowadd INDEX-PRESENT INDEX-NEW
1625 # If ${ALLOWDELETE} != "yes", then remove any entries from
1626 # INDEX-PRESENT which don't correspond to entries in INDEX-NEW.
1627 fetch_filter_allowdelete INDEX-PRESENT INDEX-NEW
1629 # If ${KEEPMODIFIEDMETADATA} == "yes", then for each entry in
1630 # INDEX-PRESENT with metadata not matching any entry in INDEX-OLD,
1631 # replace the corresponding line of INDEX-NEW with one having the
1632 # same metadata as the entry in INDEX-PRESENT.
1633 fetch_filter_modified_metadata INDEX-OLD INDEX-PRESENT INDEX-NEW
1635 # Remove lines from INDEX-PRESENT and INDEX-NEW which are identical;
1636 # no need to update a file if it isn't changing.
1637 fetch_filter_uptodate INDEX-PRESENT INDEX-NEW
1639 # Prepare to fetch files: Generate a list of the files we need,
1640 # copy the unmodified files we have into /files/, and generate
1641 # a list of patches to download.
1642 fetch_files_prepare INDEX-OLD INDEX-PRESENT INDEX-NEW
1645 fetch_files || return 1
1647 # Create and populate install manifest directory; and report what
1648 # updates are available.
1649 fetch_create_manifest || return 1
1651 # Warn about any upcoming EoL
1652 fetch_warn_eol || return 1
1655 # Make sure that all the file hashes mentioned in $@ have corresponding
1656 # gzipped files stored in /files/.
1658 # Generate a list of hashes
1665 # Make sure all the hashes exist
1667 if ! [ -f files/${HASH}.gz ]; then
1668 echo -n "Update files missing -- "
1669 echo "this should never happen."
1670 echo "Re-run '$0 fetch'."
1679 # Remove the system immutable flag from files
1681 # Generate file list
1683 cut -f 1 -d '|' > filelist
1687 if ! [ -e ${F} ]; then
1691 chflags noschg ${F} || return 1
1699 install_from_index () {
1700 # First pass: Do everything apart from setting file flags. We
1701 # can't set flags yet, because schg inhibits hard linking.
1702 sort -k 1,1 -t '|' $1 |
1704 while read FPATH TYPE OWNER GROUP PERM FLAGS HASH LINK; do
1707 # Create a directory
1708 install -d -o ${OWNER} -g ${GROUP} \
1709 -m ${PERM} ${BASEDIR}/${FPATH}
1712 if [ -z "${LINK}" ]; then
1713 # Create a file, without setting flags.
1714 gunzip < files/${HASH}.gz > ${HASH}
1715 install -S -o ${OWNER} -g ${GROUP} \
1716 -m ${PERM} ${HASH} ${BASEDIR}/${FPATH}
1719 # Create a hard link.
1720 ln -f ${LINK} ${BASEDIR}/${FPATH}
1725 ln -sfh ${HASH} ${BASEDIR}/${FPATH}
1730 # Perform a second pass, adding file flags.
1732 while read FPATH TYPE OWNER GROUP PERM FLAGS HASH LINK; do
1733 if [ ${TYPE} = "f" ] &&
1734 ! [ ${FLAGS} = "0" ]; then
1735 chflags ${FLAGS} ${BASEDIR}/${FPATH}
1740 # Remove files which we want to delete
1742 # Generate list of new files
1743 cut -f 1 -d '|' < $2 |
1746 # Generate subindex of old files we want to nuke
1747 sort -k 1,1 -t '|' $1 |
1748 join -t '|' -v 1 - newfiles |
1750 tr '|' ' ' > killfiles
1752 # Remove the offending bits
1753 while read FPATH TYPE; do
1756 rmdir ${BASEDIR}/${FPATH}
1759 rm ${BASEDIR}/${FPATH}
1762 rm ${BASEDIR}/${FPATH}
1768 rm newfiles killfiles
1771 # Update linker.hints if anything in /boot/ was touched
1772 install_kldxref () {
1774 grep -qE '^/boot/'; then
1779 # Rearrange bits to allow the installed updates to be rolled back
1780 install_setup_rollback () {
1781 if [ -L ${BDHASH}-rollback ]; then
1782 mv ${BDHASH}-rollback ${BDHASH}-install/rollback
1785 mv ${BDHASH}-install ${BDHASH}-rollback
1788 # Actually install updates
1790 echo -n "Installing updates..."
1792 # Make sure we have all the files we should have
1793 install_verify ${BDHASH}-install/INDEX-OLD \
1794 ${BDHASH}-install/INDEX-NEW || return 1
1796 # Remove system immutable flag from files
1797 install_unschg ${BDHASH}-install/INDEX-OLD \
1798 ${BDHASH}-install/INDEX-NEW || return 1
1801 install_from_index \
1802 ${BDHASH}-install/INDEX-NEW || return 1
1804 # Remove files which we want to delete
1805 install_delete ${BDHASH}-install/INDEX-OLD \
1806 ${BDHASH}-install/INDEX-NEW || return 1
1808 # Update linker.hints if anything in /boot/ was touched
1809 install_kldxref ${BDHASH}-install/INDEX-OLD \
1810 ${BDHASH}-install/INDEX-NEW
1812 # Rearrange bits to allow the installed updates to be rolled back
1813 install_setup_rollback
1818 # Rearrange bits to allow the previous set of updates to be rolled back next.
1819 rollback_setup_rollback () {
1820 if [ -L ${BDHASH}-rollback/rollback ]; then
1821 mv ${BDHASH}-rollback/rollback rollback-tmp
1822 rm -r ${BDHASH}-rollback/
1823 rm ${BDHASH}-rollback
1824 mv rollback-tmp ${BDHASH}-rollback
1826 rm -r ${BDHASH}-rollback/
1827 rm ${BDHASH}-rollback
1831 # Actually rollback updates
1833 echo -n "Uninstalling updates..."
1835 # If there are updates waiting to be installed, remove them; we
1836 # want the user to re-run 'fetch' after rolling back updates.
1837 if [ -L ${BDHASH}-install ]; then
1838 rm -r ${BDHASH}-install/
1839 rm ${BDHASH}-install
1842 # Make sure we have all the files we should have
1843 install_verify ${BDHASH}-rollback/INDEX-NEW \
1844 ${BDHASH}-rollback/INDEX-OLD || return 1
1846 # Remove system immutable flag from files
1847 install_unschg ${BDHASH}-rollback/INDEX-NEW \
1848 ${BDHASH}-rollback/INDEX-OLD || return 1
1851 install_from_index \
1852 ${BDHASH}-rollback/INDEX-OLD || return 1
1854 # Remove files which we want to delete
1855 install_delete ${BDHASH}-rollback/INDEX-NEW \
1856 ${BDHASH}-rollback/INDEX-OLD || return 1
1858 # Update linker.hints if anything in /boot/ was touched
1859 install_kldxref ${BDHASH}-rollback/INDEX-NEW \
1860 ${BDHASH}-rollback/INDEX-OLD
1862 # Remove the rollback directory and the symlink pointing to it; and
1863 # rearrange bits to allow the previous set of updates to be rolled
1865 rollback_setup_rollback
1870 #### Main functions -- call parameter-handling and core functions
1872 # Using the command line, configuration file, and defaults,
1873 # set all the parameters which are needed later.
1881 # Fetch command. Make sure that we're being called
1882 # interactively, then run fetch_check_params and fetch_run
1885 echo -n "`basename $0` fetch should not "
1886 echo "be run non-interactively."
1887 echo "Run `basename $0` cron instead."
1894 # Cron command. Make sure the parameters are sensible; wait
1895 # rand(3600) seconds; then fetch updates. While fetching updates,
1896 # send output to a temporary file; only print that file if the
1900 sleep `jot -r 1 0 3600`
1902 TMPFILE=`mktemp /tmp/freebsd-update.XXXXXX` || exit 1
1903 if ! fetch_run >> ${TMPFILE} ||
1904 ! grep -q "No updates needed" ${TMPFILE} ||
1905 [ ${VERBOSELEVEL} = "debug" ]; then
1906 mail -s "`hostname` security updates" ${MAILTO} < ${TMPFILE}
1912 # Install downloaded updates.
1914 install_check_params
1915 install_run || exit 1
1918 # Rollback most recently installed updates.
1920 rollback_check_params
1921 rollback_run || exit 1
1926 # Make sure we find utilities from the base system
1927 export PATH=/sbin:/bin:/usr/sbin:/usr/bin:${PATH}
1930 for COMMAND in ${COMMANDS}; do