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

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

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