Annotation of loncom/build/check-rpms, revision 1.3

1.1       harris41    1: #!/usr/bin/perl
                      2: #
1.3     ! harris41    3: # check-rpms, version 2.1.1
1.1       harris41    4: # Martin Siegert, SFU, siegert@sfu.ca, Feb 02
                      5: #
1.2       harris41    6: =pod
                      7: 
                      8: =head1 NAME
                      9: 
                     10: check-rpms - compare installed rpms with up-to-date distribution
                     11: 
                     12: =head1 DESCRIPTION
                     13: 
                     14: I<check-rpms> compares installed RPM packages (listed by the command
                     15: "rpm -qa") on a Linux system with an up-to-date distribution. That
                     16: distribution may either reside in a local directory (possibly NFS
                     17: mounted) or on a ftp server.  If the B<-ftp> option is specified,
                     18: I<check-rpms> retrieves directory listings from the I<ftpserver>'s
                     19: I<directory>/<arch> directories, where <arch> is set to noarch, i386,
                     20: i586, i686, and athlon consecutively. If I<ftpserver/directory> is
                     21: not specified, $FTPSERVER/$FTPUPDATES is used. The $FTPSERVER and
                     22: $FTPUPDATES variables can be set in the configuration file. If
                     23: either of the two is not set, the default server "updates.redhat.com"
                     24: and the default directory "$RHversion/en/os" is used,
                     25: where $RHversion is obtained from the /etc/redhat-release file. If
                     26: run with the B<-ftp> option, all rpm packages that need to be downloaded
                     27: (see the B<--download>, B<--recheck>, and B<--update> options) will
                     28: be downloaded into the directory specified by the B<-d> directory
                     29: option. If that option is omitted the $RPMDIR directory is used.
                     30: The $RPMDIR variable that can be set in the configuration file. If
                     31: $RPMDIR variable is not set either, the default directory
                     32: "/mnt/redhat/RedHat/RPMS" is used.
                     33: 
                     34: If the B<-ftp> is omitted, it is assumed that B<-d> I<directory> specifies
                     35: a local directory that contains up-to-date rpm packages. If B<-d>
                     36: I<directory> is omitted as well, the $RPMDIR directory is used. If
                     37: $RPMDIR is not set, the default directory "/mnt/redhat/Red-
                     38: Hat/RPMS" is used.
                     39: 
                     40: I<check-rpms> uses a lexical sort on the version string and the
                     41: release string of the package in order to decide whether the
                     42: installed package or the package form the distribution is newer.
                     43: I<check-rpms> lists packages of the distribution that are found to be
                     44: newer than the installed packages or, if B<--update> is specified,
                     45: will update the packages using the "rpm -Fvh <list of packages>"
                     46: command. In the latter case I<check-rpms> must be run as root. Fur-
                     47: thermore, the $RPMUSER variable should be set to a non-root user-
                     48: name (see the B<-c> option below). I<check-rpms> will switch to that
                     49: user and run most of the script under that user id.Only the
                     50: final "rpm -Fvh ..." command will be run as root. If $RPMUSER is
                     51: not set, the "nobody" user id will be used. It is recommended to
                     52: set $RPMUSER to an ordinary username (such as yourself). Further-
                     53: more, if a ftp server is used, create the download directory
                     54: (which is specified in the B<-d> directory option or in the $RPMDIR
                     55: variable), change the owner ship of that directory to that user,
                     56: and set the permissions to 700 before running I<check-rpms> with the
                     57: B<--update> option. Note, that B<--update> implies the B<--no-kernel>
                     58: option, i.e., I<check-rpms> refuses to update the kernel directly.
                     59: 
                     60: =cut
                     61: 
1.1       harris41   62: # ************ WARNING *****************************************************
                     63: # THIS PROGRAM IS PROVIDED "AS IS" WITHOUT
                     64: # WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLICIT.
                     65: # IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
                     66: # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     67: # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     68: # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     69: # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     70: # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     71: # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     72: # SUCH DAMAGE.
                     73: # **************************************************************************
                     74: 
                     75: # check-rpms.pl is free software; you can redistribute it and/or modify
                     76: # it under the terms of the GNU General Public License as published by
                     77: # the Free Software Foundation; either version 2 of the License, or
                     78: # (at your option) any later version.
                     79: #
                     80: # check-rpms.pl is distributed in the hope that it will be useful,
                     81: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     82: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     83: # GNU General Public License for more details:
                     84: # http://www.gnu.org/licenses/gpl.html
                     85: 
                     86: use Getopt::Long;
                     87: 
                     88: my $retval = &GetOptions("verbose|v","lm|list-missing","lq|list-questionable",
                     89:                          "dir|d=s","ftp:s","noftp","download|dl","recheck|r",
                     90:                          "nk|no-kernel","update","c=s");
                     91: 
1.2       harris41   92: =pod
                     93: 
                     94: =head1 OPTIONS
                     95: 
                     96: =over 4
                     97: 
                     98: =item B<-v> B<--verbose>
                     99: 
                    100: verbose  mode:  prints  additional  progress information on
                    101: standard output
                    102: 
                    103: =item B<-ftp> [I<ftpserver/directory>]
                    104: 
                    105: compare the installed packages with the rpm packages found
                    106: on the ftp server I<ftpserver> in the directories I<directory>/<arch>,
                    107: where arch is set to noarch, i386, i586, i686,
                    108: and athlon consecutively. If I<ftpserver/directory> is not
                    109: specified, the $FTPSERVER and $FTPUPDATES variables are
                    110: checked. These variables can be set in the configuration
                    111: file (see the B<-c> option below). If those variables are not
                    112: set either, the default server "updates.redhat.com" and the
                    113: default directory "$RHversion/en/os" is used, where $RHversion
                    114: is obtained from the I</etc/redhat-release> file.
                    115: 
                    116: =item B<-noftp>
                    117: 
                    118: use  a  local  directory as the source for new rpm packages
                    119: even if the $FTP veriable is set to 1 in the  configuration
                    120: file.
                    121: 
                    122: =item B<-d> I<directory> B<--rpm-directory> I<directory>
                    123: 
                    124: if B<-ftp> is specified download all rpm packages that need to
                    125: be downloaded into I<directory>. If B<-ftp> is not specified,
                    126: regard the rpm packages found in I<directory> as an up-to-date
                    127: distribution against which the installed packages are
                    128: compared to.
                    129: 
                    130: =item B<-lm> B<--list-missing>
                    131: 
                    132: list installed packages that do not have an equivalent in
                    133: the up-to-date distribution. This will generate lots of
                    134: output when the comparison is made with the updates directory
                    135: of a ftp server.
                    136: 
                    137: =item B<-lq> B<--list-questionable>
                    138: 
                    139: list packages for which the lexical sort algorithm does not
                    140: give a conclusive result on whether the installed package
                    141: is older than the package in the distribution. These are
                    142: packages that have version and/or release strings that contain
                    143: letters. For example, it is not absolutely clear
                    144: whether the version 1.2.3b is actually newer or older than
                    145: 1.2.3. The lexical sort would classify 1.2.3b to be newer
                    146: than 1.2.3; with B<-lq> specified the package would be listed
                    147: in any case. See also B<--recheck> below.
                    148: 
                    149: =item B<-dl> B<--download>
                    150: 
                    151: download packages from the remote ftp server that are found
                    152: to be newer than installed packages into the directory that
                    153: is specified in the B<-d> I<directory> option or in the $RPMDIR
                    154: variable or, if neither of the two are specified, into
                    155: "/mnt/redhat/RedHat/RPMS". If the download directory does
                    156: not exist, I<check-rpms> will create it.
                    157: 
                    158: =item B<-r> B<--recheck>
                    159: 
                    160: Use the "rpm -Uvh --test --nodeps <package>" command to
                    161: check all packages that have letters in their version
                    162: and/or release string; B<--recheck> implies B<--list-questionable>
                    163: (see above). At the time of writing (Feb. 2002) there
                    164: is one known case for which the lexical sort algorithm
                    165: fails to detect a new package: mutt-1.2.5.1 was released to
                    166: replace mutt-1.2.5i, however, the lexical sort algorithm
                    167: incorrectly classifies mutt-1.2.5i to be  newer  than
                    168: mutt-1.2.5.1. In this case using the B<--recheck> option is
                    169: essential. In all other cases it is not. It is nevertheless
                    170: probably a good idea to use B<--recheck> at least once in a
                    171: while. B<--recheck> can increase the run-time of I<check-rpms>
                    172: substantially, particularly if a ftp server is used. In
                    173: that case the questionable packages must be downloaded from
                    174: the server into a directory I<directory> (as specified in the
                    175: -d option or the $RPMDIR variable) which will be created,
                    176: if it does not exist.
                    177: 
                    178: =item B<-nk> B<--no-kernel>
                    179: 
                    180: do not list kernel packages. That is, kernel, kernel-smp,
                    181: kernel-enterprise, kernel-BOOT, and kernel-debug will not
                    182: be checked and listed. However, kernel-headers and kernel-source
                    183: will be checked. The B<--update> option (see below)
                    184: implies B<--no-kernel>.
                    185: 
                    186: =item B<--update>
                    187: 
                    188: update all packages that were found to have newer versions.
                    189: For this to work I<check-rpms> must be run as root and a suitable
                    190: $RPMUSER must exist (see DESCRIPTION above). It is
                    191: strongly advisable to do a dry run B<check-rpms -v -lq> before
                    192: running B<check-rpms --update>.
                    193: 
                    194: =item B<-c> I<configurationfile>
                    195: 
                    196: The optional configuration file to use. This file can be
                    197: used to specify the $RPMDIR variable, the $FTP, $FTPSERVER,
                    198: and $FTPUPDATES, variables, and the $RPMUSER variable. An
                    199: example configuration file is given below. If the B<-c> option
                    200: is omitted, I<check-rpms> will use the default configuration
                    201: file I</usr/local/etc/check-rpms.conf>, if it exists.
                    202: 
                    203: =back
                    204: 
                    205: =head1 EXAMPLES
                    206: 
                    207: =over 4
                    208: 
                    209: =item check-rpms
                    210: 
                    211: will 1) check whether /usr/local/etc/check-rpms.conf exists; 2) if
                    212: it does it will read the variables specified in that file, if it
                    213: doesn't exist, $RPMDIR is set to /mnt/redhat/RedHat/RPMS; 3) if
                    214: $RPMDIR is set, this directory will be regarded as the source of
                    215: the up-to-date distribution, unless $FTP is set to 1. In that latter
                    216: case the $FTPSERVER and $FTPUPDATES are used, if those variables are
                    217: set. Otherwise "updates.redhat.com" and "<RHversion>/en/os"
                    218: will be used; 4) the installed packages are compared
                    219: 
                    220: =item check-rpms -v -lq -d /mnt/redhat/7.1/RedHat/RPMS
                    221: 
                    222: will use the distribution in the directory /mnt/redhat/7.1/RedHat/RPMS
                    223: for comparison with the installed packages. The command
                    224: will give more detailed information on its progress and will list
                    225: the packages that need upgrading and in another section it will
                    226: list packages they may need to be upgraded.
                    227: 
                    228: =item check-rpms -v -lq -ftp updates.redhat.com/7.1/en/os
                    229: 
                    230: same as above, but the directories 7.1/en/os/noarch,
                    231: 7.1/en/os/i386, 7.1/en/os/i586, 7.1/en/os/i686, and
                    232: 7.1/en/os/athlon on updates.redhat.com will be searched for new
                    233: packages.
                    234: 
                    235: =item check-rpms -v -r --updates
                    236: 
                    237: will use the default location for updated packages (determined as
                    238: indicated in the first example); if a ftp server is used, it will
                    239: download all newer and all packages with letters in the version
                    240: and/or release strings (i.e., "questionable" packages) from that
                    241: ftp server, recheck the questionable packages, and finally update
                    242: all packages that need to be updated.
                    243: 
                    244: =back
                    245: 
                    246: =cut
                    247: 
1.1       harris41  248: if ( $retval == 0 ) {
                    249:     usage();
                    250: }
                    251: 
                    252: # executables
                    253: $FTPLS = "ncftpls";
                    254: $FTPGET = "ncftpget";
                    255: $GREP = "grep";
                    256: 
                    257: # default values
                    258: $RHversion = (split /\s/, `cat /etc/redhat-release`)[4];
                    259: $DEFCONF = "/usr/local/etc/check-rpms.conf";
                    260: $DEFRPMDIR = "/mnt/redhat/RedHat/RPMS";
                    261: $DEFFTPSERVER = "updates.redhat.com";
                    262: $DEFFTPUPDATES = "$RHversion/en/os";
                    263: $DEFRPMUSER = "nobody";
                    264: 
                    265: $RPMDIR=$DEFRPMDIR;
                    266: 
                    267: # configuration
                    268: # the configuration file should set the $RPMDIR variable and/or $FTPSERVER,
                    269: # $FTPUPDATES and $DOWNLOADDIR variables, and the $RPMUSER variable.
                    270: if ($opt_c) {
                    271:    $CONF = $opt_c;
                    272: } else {
                    273:    $CONF = $DEFCONF;
                    274: }
                    275: 
1.2       harris41  276: =pod
                    277: 
                    278: =head1 The Configuration File
                    279: 
                    280: All variables must be defined using perl syntax, i.e., in the form
                    281: 
                    282: $variable = value;
                    283: 
                    284: (do not forget the semicolon at the  end  of  a  line).   Comments
                    285: start with "#" and blank lines may be included as well.
                    286: 
                    287: Example configuration file:
                    288: 
                    289:  # check-rpms configuration file
                    290: 
                    291:  # $RPMDIR is the directory where up-to-date RPMs can be found and/or
                    292:  # rpm packages are downloaded into.
                    293:  $RPMDIR = "/mnt/redhat/RedHat/RPMS";
                    294: 
                    295:  # $RPMUSER is the user name that check-rpms switches to for most of
                    296:  # the script when run as root
                    297:  $RPMUSER = "joe";
                    298: 
                    299:  # $FTPSERVER and $FTPUPDATES are the hostname of a ftp server and the
                    300:  # directory where RPM updates can be found without the <arch> directory.
                    301:  # I.e., $FTPUPDATES should be set to something like pub/7.2, if the RPMs
                    302:  # are located in pub/7.2/i386, pub/7.2/i686, etc.
                    303:  # $FTPSERVER and $FTPUPDATES are used if -ftp is specified or if the following
                    304:  # line is uncommented.
                    305:  # $FTP = 1;
                    306:  $FTPSERVER = "updates.redhat.com";
                    307:  $FTPUPDATES = "7.2/en/os";
                    308: 
                    309: =cut
                    310: 
1.1       harris41  311: if ( -f $CONF) {
                    312:    require($CONF);
                    313: } else {
                    314:    $FTPSERVER = $DEFFTPSERVER;
                    315:    $FTPUPDATES = $DEFFTPUPDATES;
                    316: }
                    317: 
                    318: # check whether we are running as root
                    319: if ($< == 0){
                    320:    if (! $RPMUSER) {
                    321:       $RPMUSER = $DEFRPMUSER;
                    322:    }
                    323:    $RPMUID = getpwnam($RPMUSER);
                    324:    if (! $RPMUID) {
                    325:       die "You do not seem to have a $RPMUSER user on your system.\nSet the \$RPMUSER variable in the $CONF configuration file to a non-root user.\n";
                    326:    }
                    327:    if ($RPMUID == 0) {
                    328:       die "You must set the \$RPMUSER variable in $CONF to a non-root user.\n";
                    329:    }
                    330:    # switch to $RPMUID
                    331:    $> = $RPMUID;
                    332:    if ($> != $RPMUID) { die "switching to $RPMUID uid failed.\n" }
                    333: }
                    334: 
                    335: # command-line arguments
                    336: $verbose         = $opt_verbose;
                    337: $list_missing    = $opt_lm;
                    338: $questionable    = $opt_lq;
                    339: $no_kernel       = $opt_nk;
                    340: $download        = $opt_download;
                    341: $recheck         = $opt_recheck;
                    342: $update          = $opt_update;
                    343: 
                    344: if (defined $opt_update && $< != 0) {
                    345:     die "You must be root in order to update rpms.\n";
                    346: }
                    347: 
                    348: if ( defined $opt_dir ){
                    349:    $RPMDIR = $opt_dir;
                    350: }
                    351: 
                    352: if (defined $opt_ftp && defined $opt_noftp) {
                    353:    die "Setting -ftp and -noftp does not make sense, does it?\n";
                    354: }
                    355: 
                    356: if (defined $opt_noftp) { $FTP = 0; }
                    357: 
                    358: if (defined $opt_ftp || $FTP) {
                    359:    $ftp = 1;
                    360:    if ( $opt_ftp ) {
                    361:       $_ = $opt_ftp;
                    362:       ($FTPSERVER, $FTPUPDATES) = m/^([^\/]+)\/(.*)$/;
                    363:    } elsif ( ! ($FTPSERVER && $FTPUPDATES)) {
                    364:       $FTPSERVER = $DEFFTPSERVER;
                    365:       $FTPUPDATES = $DEFFTPUPDATES;
                    366:    }
                    367: 
                    368:    if (defined $opt_update){
                    369:       $download=1;
                    370:    }
                    371: 
                    372:    if ($download || $recheck) {
                    373:        if ( ! -d $RPMDIR) {
1.3     ! harris41  374:           if ($verbose) { print "Creating $RPMDIR ...\n"; }
        !           375:           if ($< == 0) {
        !           376:              $retval = system("su $RPMUSER -c \'mkdir -p $RPMDIR\'; chmod 700 $RPMDIR");
        !           377:           } else {
        !           378:              $retval = system("mkdir -p $RPMDIR; chmod 700 $RPMDIR");
        !           379:           }
1.1       harris41  380:           if ($retval) { die "error: could not create $RPMDIR\n"; }
                    381:       }
                    382:    }
                    383: } elsif ( (! -d $RPMDIR) || system("ls $RPMDIR/*.rpm > /dev/null 2>&1")) {
                    384:    die "Either $RPMDIR does not exist or it does not contain any packages.\n";
                    385: }
                    386: 
                    387: if ($recheck) {
                    388:    $questionable=1;
                    389: }
                    390: 
                    391: if (defined $opt_update || defined $opt_nk) {
                    392:     $no_kernel=1;
                    393: }
                    394: 
                    395: $PROC = `grep -i athlon /proc/cpuinfo`;
                    396: if ( ! "$PROC" ) {
                    397:     $PROC = `uname -m`;
                    398:     chomp($PROC);
                    399: } else {
                    400:     $PROC = "athlon";
                    401: }
                    402: 
                    403: @ARCHITECTURES = ("noarch", "i386", "i586", "i686");
                    404: if ( $RHversion > 7.0 ){ 
                    405:     push(@ARCHITECTURES, "athlon");
                    406: }
                    407: 
                    408: # get the local list of installed packages
                    409: 
                    410: if ($verbose) {
                    411:    print "updates for $PROC processor, RH $RHversion\n";
                    412:    print "Getting list of installed packages\n";
                    413: }
                    414: 
                    415: if ($< == 0) {
                    416:    @local_rpm_list = `su $RPMUSER -c 'rpm -qa'`;
                    417: } else {
                    418:    @local_rpm_list = `rpm -qa`;
                    419: }
                    420: chop(@local_rpm_list);
                    421: 
                    422: %local_rpm = %remote_rpm = ();
                    423: 
                    424: for (@local_rpm_list) {
                    425: #    good place to test the regular expressions...
                    426: #    ($pkg, $ver, $release) = m/^(.*)-([^-]*)-([^-]+)/;
                    427: #    print "$_\t->$pkg, $ver, $release\n";
                    428: 
                    429:     my ($pkg, $pver) = m/([^ ]*)-([^-]+-[^-]+)/;
                    430:     $local_rpm{$pkg} = $pver;
                    431: }
                    432: 
                    433: # now connect to the remote host
                    434: 
                    435: my @templist;
                    436: if ($ftp) {
                    437:    if ( `rpm -q ncftp --pipe "grep 'not installed'"` ) {
                    438:       die "you must have the ncftp package installed in order to use a\n",
                    439:           "ftp server with check-rpms.\n";
                    440:    }
                    441:    $SOURCE = $FTPSERVER;
                    442:    for (@ARCHITECTURES) {
                    443:       my $FTPDIR = "$FTPUPDATES/$_";
                    444:       if ($verbose) {
                    445:          print ("Getting package lists from $FTPSERVER/$FTPDIR ...\n");
                    446:       }
                    447:       push(@templist, grep(/\.rpm$/, `$FTPLS -x "-1a" "ftp://$FTPSERVER/$FTPDIR/"`));
                    448:       if ($?) { print STDERR "$FTPLS failed with status ",$?/256,".\n"; }
                    449:    }
                    450: } else {
                    451:    $SOURCE = $RPMDIR;
                    452:    if ($verbose) {
                    453:        print ("Getting package lists from $RPMDIR ...\n");
                    454:    }
                    455:    @templist = grep(/\.rpm$/, `(cd $RPMDIR;ls -1)`);
                    456: }
                    457: 
                    458: #
                    459: # If two versions of the same RPM appear with different architectures
                    460: # and/or different versions, the right one must be found.
                    461: #
                    462: 
                    463: $giveup = 0;
                    464: for (@templist) {
                    465:    ($rpm, $pkg, $pver, $arch) = m/(([^ ]*)-([^- ]+-[^-]+\.(\w+)\.rpm))/;
1.3     ! harris41  466:    if (! defined $local_rpm{$pkg}) { next; }
1.1       harris41  467:    if ($remote_rpm{$pkg}) {
                    468:       # problem: there are several versions of the same package.
                    469:       # this means that the package exists for different architectures
                    470:       # (e.g., kernel, glibc, etc.) and/or that the remote server
                    471:       # has several versions of the same package in which case the
                    472:       # latest version must be picked.
                    473:       my ($pkg1) = ($remote_rpm{$pkg} =~ m/([^-]+-[^-]+)\.\w+.rpm/);
                    474:       my ($pkg2) = ($pver =~ m/([^-]+-[^-]+)\.\w+.rpm/);
                    475:       my ($vcmp, $qflag) = cmp_versions($pkg1, $pkg2);
                    476:       if ($qflag && $questionable) {
                    477:          # cannot decide which of the two is newer - what should we do?
                    478:          # print a warning that lists the two rpms.
                    479:          # If running with --update, both packages must be rechecked with 
                    480:          # rpm -qp --queryformat '%{SERIAL}' <pkg>
                    481:          if ($recheck || $update) {
                    482:             my $decision = pkg_compare("$pkg-$remote_rpm{$pkg}",$rpm, $vcmp);
                    483:             if ($decision < 0) {
                    484:                # an error in the ftp download routine accured: giveup
                    485:                $remote_rpm{$pkg} = undef;
                    486:                $giveup = 1;
                    487:             } elsif ($decision > 0) {
                    488:                # second package is newer
                    489:                $remote_rpm{$pkg} = $pver;
                    490:             }
                    491:             next;
                    492:          } else {
                    493:             mulpkg_msg("$pkg-$remote_rpm{$pkg}", $rpm, $vcmp);
                    494:             print "** check whether this is correct or rerun with --recheck option.\n";
                    495:             if ($vcmp < 0) {
                    496:                $remote_rpm{$pkg} = $pver;
                    497:             }
                    498:          }
                    499:       }
                    500:       if ($vcmp == 0) {        
                    501:          # versions are equal: must be different architecture
                    502:          # procedure to select the correct architecture:
                    503:          # if $PROC = athlon: if available use $arch = athlon (exist for
                    504:          # RH 7.1 or newer) otherwise use i686
                    505:          # if $PROC = ix86: choose pkg with $PROC cmp $arch >= 0 and
                    506:          # $arch cmp $prev_arch = 1
                    507:          $_ = $remote_rpm{$pkg};
                    508:          ($prev_arch) =  m/.*\.(\w+)\.rpm$/;
                    509:          if (cmp_arch($arch,$prev_arch)) { $remote_rpm{$pkg} = $pver };
                    510:       } elsif ($vcmp < 0) {    # second rpm is newer
                    511:          $remote_rpm{$pkg} = $pver;
                    512:       }
                    513:    } else {
                    514:       $remote_rpm{$pkg} = $pver;
                    515:    }
                    516: }
                    517: 
                    518: if ($giveup && defined $opt_update) {
                    519:    die "Multiple versions of the same package were found on the server.\n",
                    520:        "However, due to ftp download problems it could not be verified\n",
                    521:        "which of the packages are the most recent ones.\n",
                    522:        "If the choices specified above appear to be correct, rerun check-rpms\n",
                    523:        "without the -lq (or --list-questionable) option. Otherwise, fix the download\n",
                    524:        "problems or install those packages separately first.\n";
                    525: }
                    526: 
                    527: #
                    528: # check for UPDated and DIFferent packages...
                    529: #
                    530: 
                    531: for (@local_rpm_list) {
                    532:     my ($pkg,  $version) = m/^([^ ]*)-([^-]+-[^-]+)$/;
                    533:     if (! $pkg) { print "Couldn't parse $_\n"; next; }
                    534:     if ($no_kernel) {
                    535:        if ($pkg eq 'kernel' || $pkg eq 'kernel-smp'
                    536:            || $pkg eq 'kernel-enterprise' || $pkg eq 'kernel-BOOT'
                    537:            || $pkg eq 'kernel-debug') { next; }
                    538:     }
                    539:     if (defined $remote_rpm{$pkg}) { 
                    540:         # this package has an update
                    541: 	my ($rversion) = ($remote_rpm{$pkg} =~ m/([^-]+-[^-]+)\.\w+.rpm/);
                    542: 	my $rpm = ($pkg . '-' . $remote_rpm{$pkg});
                    543: 	my ($vcmp,$qflag) = cmp_versions($version, $rversion);
                    544: 	if ( $qflag && $questionable ) {
                    545:             # at least one of the version strings contains letters
                    546:             push(@q_updates, $rpm);
                    547:         } elsif ( $vcmp < 0 ) {
                    548:             # local version is lower
1.3     ! harris41  549:             push(@updates, $rpm);
1.1       harris41  550: 	}
                    551:     } elsif ($list_missing) {
                    552: 	print "Package '$pkg' missing from remote repository\n";
                    553:     }
                    554: }
                    555: 
                    556: if ($recheck && @q_updates) {
                    557:    if ($ftp) {    
                    558:       for (@q_updates) {
                    559:          ($arch) = m/[^ ]*-[^- ]+-[^-]+\.(\w+)\.rpm/;
                    560:          push(@ftp_files, "$FTPUPDATES/$arch/$_");
                    561:       }
                    562:       if ($verbose) {
                    563:          print "Getting questionable packages form $FTPSERVER ...\n";
                    564:       }
                    565:       my $status = system("$FTPGET $FTPSERVER $RPMDIR @ftp_files");
                    566:       if ($status) {
                    567:          if ($< == 0) {
                    568:             # if we are running as root exit to avoid symlink attacks, etc.
                    569:             die "$FTPGET failed with status ", $status/256, ".\n";
                    570: 	 } else {
                    571:             print STDERR "warning: $FTPGET failed with status ", $status/256, ".\n";
                    572:          } 
                    573:       }
                    574:    }
                    575:    for (@q_updates) {
                    576:       if ($verbose) {print "** rechecking $_ ... ";}
                    577:       my $errmsg = `rpm -Uvh --test --nodeps --pipe 'grep -v ^Preparing' $RPMDIR/$_ 2>&1`;
                    578:       if (! $errmsg) {
                    579:          # no error message, i.e., the rpm is needed.
                    580:           push(@updates,$_);
                    581:           if ($verbose) {print "needed!\n";}
                    582:       } elsif ($verbose) {
                    583:           print "not needed:\n$errmsg\n";
                    584:       }
                    585:    }
                    586:    @q_updates=();
                    587: }
                    588:        
                    589: #
                    590: # print list of new files and download ...
                    591: #
                    592: 
                    593: @updates = sort @updates;
                    594: if (@updates) {
                    595:     if ($verbose) {
                    596:        print "\nRPM files to be updated:\n\n";
                    597:     }
                    598:     for (@updates) {
                    599:        print "$_\n";
                    600:     }
                    601:     if ($download) {
                    602:        @ftp_files=();
                    603:        for (@updates) {
                    604:           ($arch) = m/[^ ]*-[^- ]+-[^-]+\.(\w+)\.rpm/;
                    605:           push(@ftp_files, "$FTPUPDATES/$arch/$_");
                    606:        }
                    607:        if ($verbose) {
                    608:           print "starting downloads ... \n";
                    609:        }
                    610:        my $status = system("$FTPGET $FTPSERVER $RPMDIR @ftp_files");
                    611:        if ($status) {
                    612:           if ($< == 0) {
                    613:              # if we are running as root exit to avoid symlink attacks, etc.
                    614:              die "$FTPGET failed with status ", $status/256, ".\n";
                    615: 	  } else {
                    616:              print STDERR "warning: $FTPGET failed with status ", $status/256, ".\n";
                    617:           } 
                    618:        } elsif ($verbose) {
                    619:           print "... done.\n";
                    620:        }
                    621:     }           
                    622: }
                    623: 
                    624: @q_updates = sort @q_updates;
                    625: if (@q_updates && $questionable) {
                    626:     if ($verbose) {
                    627:        print "\nRPM files that may need to be updated:\n\n";
                    628:        for (@q_updates) {
                    629:           my ($old) = m/^([^ ]*)-[^-]+-[^-]+\.\w+\.rpm$/;
                    630:           $old = `rpm -q $old`;
                    631:           chomp($old);
                    632:           print "upgrade ", $old, " to ", $_, " ?\n";
                    633:        }
                    634:     } else {
                    635:        for (@q_updates) {
                    636:           print "$_\n";
                    637:        }
                    638:     }
                    639:     if ($download) {
                    640:        @ftp_files=();
                    641:        for (@updates) {
                    642:           ($arch) = m/[^ ]*-[^- ]+-[^-]+\.(\w+)\.rpm/;
                    643:           push(@ftp_files, $FTPUPDATES/$arch/$_);
                    644:        }
                    645:        if ($verbose) {
                    646:           print "starting downloads ... \n";
                    647:           system("$FTPGET $FTPSERVER $$RPMDIR @ftp_files");
                    648:           print "... done.\n";
                    649:        } else {
                    650:           system("$FTPGET $FTPSERVER $$RPMDIR @ftp_files");
                    651:        }
                    652:     }           
                    653: }
                    654: 
                    655: if ($verbose && !(@updates || @q_updates)) {
                    656:     print "No new updates are available in $SOURCE\n";
                    657: }
                    658: 
                    659: if ($opt_update) {
                    660:     if (@q_updates){
                    661:        push(@updates,@q_updates);
                    662:     }
                    663:     if (@updates) {
                    664:        if ($verbose) {
                    665:           print "Running rpm -Fvh ...\n";
                    666:        }
                    667:        # switch to UID=0
                    668:        $> = $<;
                    669:        system("(cd $RPMDIR;rpm -Fvh @updates)");
                    670:    }
                    671: }
                    672: 
                    673: # download routine
                    674: sub ftp_download {
                    675:    my ($FTPSERVER, $FTPDIR, $downloaddir, @packages) = @_;
                    676:    my @ftp_packages=();
                    677:    for (@packages) {
                    678:        my ($arch) = m/[^ ]*-[^-]+-[^-]*\.(\w+)\.rpm$/;
                    679:        push(@ftp_packages,"$FTPDIR/$arch/$_");
                    680:     }
                    681:     my $status = system("$FTPGET $FTPSERVER $downloaddir @ftp_packages");
                    682:     return $status;
                    683: }
                    684: 
                    685: sub pkg_compare($$$) {
                    686:    my ($pkg1, $pkg2, $cmp) = @_;
                    687:    if (defined $opt_ftp) {
                    688:       if ($verbose) {
                    689:          my ($pkg) = ($pkg1 =~ /([^ ]*)-[^-]+-[^-]+\.\w+\.rpm/);
                    690:          print "The ftp server provides multiple versions of the $pkg package.\n",
                    691:                "Downloading $pkg1 and $pkg2 in order to find out which is newer.\n";
                    692:       }
                    693:       my $status = ftp_download($FTPSERVER, $FTPUPDATES, $RPMDIR, ($pkg1, $pkg2));
                    694:       if ($status) {
                    695:          # at this point just give up ...
                    696:          print STDERR "** $FTPGET failed with status ", $status/256, ".\n";
                    697:          mulpkg_msg($pkg1, $pkg2, $cmp);
                    698:          return -1;
                    699:       }
                    700:    }
                    701:    my $serial1 = `rpm -qp --queryformat '%{SERIAL}' $RPMDIR/$pkg1`;
                    702:    my $serial2 = `rpm -qp --queryformat '%{SERIAL}' $RPMDIR/$pkg2`;
1.3     ! harris41  703:    if ($serial2 > $serial1) {
        !           704:       remove_pkg("$RPMDIR/$pkg1");
        !           705:       return 1;
        !           706:    } else {
        !           707:       remove_pkg("$RPMDIR/$pkg2");
        !           708:       return 0;
        !           709:    }
        !           710: }
        !           711: 
        !           712: sub remove_pkg($) {
        !           713:    my ($pkg) = @_;
        !           714:    if ($verbose) {
        !           715:       print "Removing $pkg ...\n";
        !           716:    }
        !           717:    my $status = system("rm -f $pkg");
        !           718:    if ($status) {
        !           719:       printf STDERR "error: could not remove $pkg. You must remove this file before updating.\n";
        !           720:       if ($update) { $giveup = 1; }
        !           721:    }
1.1       harris41  722: }
                    723: 
                    724: sub mulpkg_msg($$$) {
                    725:    my ($pkg1, $pkg2, $cmp) = @_;
                    726:    print "** The server provides two versions of the same package:\n",
                    727:          "** $pkg1 and $pkg2.\n";
                    728:    if ($cmp > 0) {
                    729:        print "** It appears that $pkg-$remote_rpm{$pkg} is newer.\n"
                    730:    } else {
                    731:        print "** It appears that $pkg-$pver is newer.\n";
                    732:    }
                    733: }
                    734: 
                    735: #############################################################################
                    736: #
                    737: # Version comparison utilities
                    738: #
                    739: 
                    740: sub hack_version($) {
                    741:     my ($pver) = @_;
                    742:     $pver =~ s/(\d+)/sprintf("%08d", $1)/eg; # pad numbers with leading zeros to make alphabetical sort do the right thing
                    743:     $pver =  (sprintf "%-80s", $pver);	     # pad with spaces so that "3.2.1" is greater than "3.2"
                    744:     return $pver;
                    745: }
                    746: 
                    747: sub cmp_versions($$) {
                    748:     my ($pkg1, $pkg2) = @_;
                    749: 
                    750:     # shortcut if they're obviously the same.
                    751:     return (0,0) if ($pkg1 eq $pkg2);
                    752: 
                    753:     # split into version and release
                    754:     my ($ver1, $rel1) = ($pkg1 =~ m/([^-]+)-([^-]+)/);
                    755:     my ($ver2, $rel2) = ($pkg2 =~ m/([^-]+)-([^-]+)/);
                    756: 
                    757:     if ($ver1 ne $ver2) {
                    758:        my $qflag = ((grep /[A-z]/, $ver1) || (grep /[A-z]/, $ver2));
                    759:        $ver1 = hack_version($ver1);
                    760:        $ver2 = hack_version($ver2);
                    761:        return ($ver1 cmp $ver2, $qflag);
                    762:     } else {
                    763:        my $qflag = ((grep /[A-z]/, $rel1) || (grep /[A-z]/, $rel2));
                    764:        $rel1 = hack_version($rel1);
                    765:        $rel2 = hack_version($rel2);
                    766:        return ($rel1 cmp $rel2, $qflag);
                    767:     }
                    768: }
                    769: 
                    770: sub cmp_arch($$) {
                    771:     my ($arch1, $arch2) = @_;
                    772:     my $retval = 0;
                    773:     $archcmp = ($arch1 cmp $arch2) > 0;
                    774:     if ( "$PROC" eq "athlon" ) {
                    775:        if ( "$arch2" ne "athlon" 
                    776:               && ( "$arch1" eq "athlon" || $archcmp )){
                    777: 	   $retval = 1;
                    778:        }
                    779:     } elsif ( $archcmp && ($PROC cmp $arch1) >= 0 ) {
                    780:        $retval = 1;
                    781:     }
                    782:     return $retval;
                    783: }
                    784: 
                    785: # @tests = ('3.2', '3.2',
                    786: #           '3.2a', '3.2a',
                    787: #           '3.2', '3.2a',
                    788: #           '3.2', '3.3',
                    789: #           '3.2', '3.2.1',
                    790: #           '1.2.5i', '1.2.5.1',
                    791: #           '1.6.3p6', '1.6.4');
                    792: # 
                    793: # while (@tests) {
                    794: #     $a = shift(@tests);
                    795: #     $b = shift(@tests);
                    796: #     printf "%-10s < %-10s = %d\n", $a, $b, cmp_versions($a, $b);
                    797: # }
                    798: #
                    799: # And the correct output is...
                    800: #
                    801: #     3.2        < 3.2        = 0
                    802: #     3.2a       < 3.2a       = 0
                    803: #     3.2        < 3.2a       = -1
                    804: #     3.2        < 3.3        = -1
                    805: #     3.2        < 3.2.1      = -1
                    806: #     1.2.5i     < 1.2.5.1    = -1
                    807: #     1.6.3p6    < 1.6.4      = -1
                    808: #
                    809: # the lexical sort does not give the correct result in the second to last case.
                    810: 
                    811: 
                    812: sub usage(){
                    813:    die "usage: check-rpms [-v | --verbose]  [-d directory | --dir directory]\n",
                    814:        "                  [-ftp [server/directory]] [-noftp] [-lm | --list-missing]\n",
                    815:        "                  [-lq | --list-questionable] [-r | --recheck ]\n",
                    816:        "                  [-nk | --no-kernel] [--update] [-c configurationfile]\n";
                    817: }
1.2       harris41  818: 
                    819: =pod
                    820: 
                    821: =head1 SEE ALSO
                    822: 
                    823: rpm(8), ncftpls(1), ncftpget(1)
                    824: 
                    825: =head1 AUTHOR
                    826: 
                    827: Martin Siegert, Simon Fraser University, siegert@sfu.ca
                    828: 
                    829: =cut

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>