Changeset af79b74 for tests


Ignore:
Timestamp:
Mar 30, 2013 9:52:01 PM (7 years ago)
Author:
kytv <kytv@…>
Branches:
master
Children:
d5443a3
Parents:
bfc3278
Message:

checkcerts.sh: cross platform support on *NIX

  • function to convert dates to Julian to perform date calculation if GNU date is not present.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • tests/scripts/checkcerts.sh

    rbfc3278 raf79b74  
    1919
    2020
    21 if [ $(which openssl) ]; then
    22     OPENSSL=1
    23 elif [ $(which certtool) ]; then : ;else
    24     echo "ERROR: Neither certtool nor openssl were found..." >&2
    25     exit 1
    26 fi
     21date2julian() {
     22    # Julian date conversion adapted from a post (its code released into the public
     23    # domain) by Tapani Tarvainen to comp.unix.shell (1998) for portability
     24    # (e.g. using 'expr' instead of requiring Bash, ksh, or zsh).
     25    #   $1 = Month
     26    #   $2 = Day
     27    #   $3 = Year
    2728
    28 # This "grouping hack" is here to prevent errors from being displayed with the
    29 # original Bourne shell (Linux shells don't need the {}s
    30 if { date --help;} >/dev/null 2>&1 ; then
    31     HAVE_GNUDATE=1
    32 fi
     29    if [ "${1}" != "" ] && [ "${2}" != ""  ] && [ "${3}" != "" ]; then
     30        ## Because leap years add a day at the end of February,
     31        ## calculations are done from 1 March 0000 (a fictional year)
     32        d2j_tmpmonth=$(expr 12 \* $3 + $1 - 3)
     33
     34        ## If it is not yet March, the year is changed to the previous year
     35        d2j_tmpyear=$(expr ${d2j_tmpmonth} / 12)
     36
     37        ## The number of days from 1 March 0000 is calculated
     38        ## and the number of days from 1 Jan. 4713BC is added
     39        expr \( 734 \* ${d2j_tmpmonth} + 15 \) / 24 - 2 \* ${d2j_tmpyear} + ${d2j_tmpyear} / 4 - ${d2j_tmpyear} / 100 + ${d2j_tmpyear} / 400 + $2 + 1721119
     40    else
     41        # We *really* shouldn't get here
     42        echo 0
     43    fi
     44}
     45
     46getmonth() {
     47    case ${1} in
     48        Jan)
     49            echo 1
     50            ;;
     51        Feb)
     52            echo 2
     53            ;;
     54        Mar)
     55            echo 3
     56            ;;
     57        Apr)
     58            echo 4
     59            ;;
     60        May)
     61            echo 5
     62            ;;
     63        Jun)
     64            echo 6
     65            ;;
     66        Jul)
     67            echo 7
     68            ;;
     69        Aug)
     70            echo 8
     71            ;;
     72        Sep)
     73            echo 9
     74            ;;
     75        Oct)
     76            echo 10
     77            ;;
     78        Nov)
     79            echo 11
     80            ;;
     81        Dec)
     82            echo 12
     83            ;;
     84          *)
     85            echo  0
     86            ;;
     87    esac
     88}
    3389
    3490checkcert() {
    3591    if [ $OPENSSL ]; then
     92        # OpenSSL's format: Mar  7 16:08:35 2022 GMT
    3693        DATA=$(openssl x509 -enddate -noout -in $1| cut -d'=' -f2-)
    3794    else
     95        # Certtool's format: Mon Mar 07 16:08:35 UTC 2022
    3896        DATA=$(certtool -i < "$1" | sed -e '/Not\sAfter/!d' -e 's/^.*:\s\(.*\)/\1/')
     97        # The formatting is normalized for passing to the date2julian function (if needed)
     98        set -- `echo $DATA`
     99        DATA="$2 $3 $4 $6 GMT"
    39100    fi
    40     # While this isn't strictly needed it'll ensure that the output is consistent,
    41     # regardles of the tool used. Dates/times are formatting according to OpenSSL's output
    42     # since this available by default on most systems.
    43     if [ -n "$HAVE_GNUDATE" ]; then
    44         LANG=C date -u -d "$(echo $DATA)" '+%b %d %H:%M:%S %Y GMT'
    45     else
    46         echo $DATA
    47     fi
     101    echo $DATA
    48102}
    49103
    50 compute_dates() {
    51     # Date computations currently depend on GNU date(1).
    52     # If run on a non-Linux system just print the expiration date.
    53     # TODO Cross-platform date calculation support
    54     if [ -n "$HAVE_GNUDATE" ]; then
    55         SECS=$(date -u -d "$EXPIRES" '+%s')
    56         DAYS="$(expr \( $SECS - $NOW \) / 86400)"
     104print_status() {
    57105        if [ $DAYS -ge $SOON ]; then
    58106            echo "Expires in $DAYS days ($EXPIRES)"
     
    74122            FAIL=1
    75123        fi
    76     else
    77         echo $EXPIRES
    78     fi
    79124}
    80125
     126compute_dates() {
     127    if [ -n "$HAVE_GNUDATE" ]; then
     128        SECS=$(date -u -d "$EXPIRES" '+%s')
     129        DAYS="$(expr \( $SECS - $NOW \) / 86400)"
     130    else
     131        set -- `echo $EXPIRES`
     132        # date2julian needs the format mm dd yyyy
     133        SECS=$(date2julian `getmonth $1` $2 $4)
     134        DAYS=$(expr $SECS - $NOW)
     135    fi
     136    print_status
     137}
     138
     139# This "grouping hack" is here to prevent errors from being displayed with the
     140# original Bourne shell (Linux shells don't need the {}s
     141if { date --help;} >/dev/null 2>&1 ; then
     142    HAVE_GNUDATE=1
     143    NOW=$(date -u '+%s')
     144else
     145    NOW=$(date2julian `date -u '+%m %d %Y'`)
     146fi
     147
     148if [ $(which openssl) ]; then
     149    OPENSSL=1
     150elif [ $(which certtool) ]; then : ;else
     151    echo "ERROR: Neither certtool nor openssl were found..." >&2
     152    exit 1
     153fi
     154
    81155cd `dirname $0`/../../installer/resources/certificates
    82 
    83 NOW=$(date -u '+%s')
    84156
    85157for i in *.crt
Note: See TracChangeset for help on using the changeset viewer.