openpkg/license.sh

Mon, 28 Jan 2013 17:37:18 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Mon, 28 Jan 2013 17:37:18 +0100
changeset 758
a2c6460cfb16
permissions
-rw-r--r--

Correct socket error reporting improvement with IPv6 portable code,
after helpful recommendation by Saúl Ibarra Corretgé on OSips devlist.

     1 #!@l_prefix@/lib/openpkg/bash
     2 ##
     3 ##  uuid -- OpenPKG UUID Update Utility
     4 ##  Copyright (c) 2000-2012 OpenPKG GmbH <http://openpkg.com/>
     5 ##
     6 ##  This software is property of the OpenPKG GmbH, DE MUC HRB 160208.
     7 ##  All rights reserved. Licenses which grant limited permission to use,
     8 ##  copy, modify and distribute this software are available from the
     9 ##  OpenPKG GmbH.
    10 ##
    11 ##  THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
    12 ##  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
    13 ##  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    14 ##  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
    15 ##  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    16 ##  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    17 ##  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
    18 ##  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    19 ##  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
    20 ##  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
    21 ##  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    22 ##  SUCH DAMAGE.
    23 ##
    25 #   configuration
    26 prefix="@l_prefix@"
    27 musr="@l_musr@"
    28 mgrp="@l_mgrp@"
    30 #   minimum command line parsing
    31 opt_v=no
    32 while [ 1 ]; do
    33     case "$1" in
    34         -v | --verbose ) opt_v=yes; shift ;;
    35         * ) break ;;
    36     esac
    37 done
    39 #   determine temporary directory
    40 tmpdir="${TMPDIR-/tmp}"
    42 #   helper function for checking the signature
    43 valid_signature () {
    44     script="%{lua: print(rpm.signature(\"$1\", nil,"
    45     script="$script \"$prefix/etc/openpkg/openpkg.com.pgp\","
    46     script="$script \"7D121A8FC05DC18A4329E9EF67042EC961B7AE34\")) }"
    47     result=`$prefix/bin/openpkg rpm --eval "$script" 2>/dev/null || true`
    48     if [ ".$result" == .true ]; then
    49         return 0
    50     else
    51         return 1
    52     fi
    53 }
    55 #   display usage help
    56 do_help () {
    57     echo "openpkg license help .......................... show usage help (this text)"
    58     echo "openpkg license install <id> <file>|<url>|- ... install a new license"
    59     echo "openpkg license update <id> <file>|<url>|- .... update an installed license"
    60     echo "openpkg license uninstall <id> ................ uninstall an installed license"
    61     echo "openpkg license activate <id> ................. activate an installed license"
    62     echo "openpkg license view <id> ..................... view an installed license"
    63     echo "openpkg license list .......................... list all installed licenses"
    64     echo "openpkg license active ........................ list the currently activated license"
    65     echo "openpkg license sanity ........................ sanity check installed license"
    66 }
    68 #   install license
    69 do_install () {
    70     if [ $# -ne 2 ]; then
    71         echo "openpkg:license:USAGE: openpkg license install <id> <file>|-" 1>&2
    72         exit 1
    73     fi
    74     id="$1"
    75     src="$2"
    76     case "$src" in
    77         http:* | https:* | ftp:* )
    78             tmp="$tmpdir/openpkg.license.txt"
    79             ( rm -f $tmp >/dev/null 2>&1 || true
    80               set -o noclobber
    81               $prefix/lib/openpkg/curl -s -q -f -L $src >$tmp
    82             ) || exit $?
    83             src="$tmp"
    84             ;;
    85         "-" )
    86             src="$tmpdir/openpkg.license.txt"
    87             ( rm -f $src >/dev/null 2>&1 || true
    88               set -o noclobber
    89               cat >$src
    90             ) || exit $?
    91             ;;
    92     esac
    93     if [ -f $prefix/etc/openpkg/license.d/$id ]; then
    94         echo "openpkg:license:ERROR: license with id \"$id\" already exists -- uninstall first" 1>&2
    95         exit 1
    96     fi
    97     if ! valid_signature $src; then
    98         echo "openpkg:license:ERROR: invalid signature on license file" 1>&2
    99         exit 1
   100     fi
   101     if [ ! -w $prefix/etc/openpkg/license.d ]; then
   102         echo "openpkg:license:ERROR: unable to store signature -- permission problems?" 1>&2
   103         exit 1
   104     fi
   105     cat $src >$prefix/etc/openpkg/license.d/$id || exit $?
   106     chown $musr:$mgrp $prefix/etc/openpkg/license.d/$id >/dev/null 2>&1 || true
   107 }
   109 #   update license
   110 do_update () {
   111     if [ $# -ne 2 ]; then
   112         echo "openpkg:license:USAGE: openpkg license update <id> <file>|-" 1>&2
   113         exit 1
   114     fi
   115     id="$1"
   116     src="$2"
   117     case "$src" in
   118         http:* | https:* | ftp:* )
   119             tmp="$tmpdir/openpkg.license.txt"
   120             ( rm -f $tmp >/dev/null 2>&1 || true
   121               set -o noclobber
   122               $prefix/lib/openpkg/curl -s -q -f -L $src >$tmp
   123             ) || exit $?
   124             src="$tmp"
   125             ;;
   126         "-" )
   127             src="$tmpdir/openpkg.license.txt"
   128             ( rm -f $src >/dev/null 2>&1 || true
   129               set -o noclobber
   130               cat >$src
   131             ) || exit $?
   132             ;;
   133     esac
   134     if [ ! -f $prefix/etc/openpkg/license.d/$id ]; then
   135         echo "openpkg:license:ERROR: license with id \"$id\" does not exist -- install first to update" 1>&2
   136         exit 1
   137     fi
   138     if ! valid_signature $src; then
   139         echo "openpkg:license:ERROR: invalid signature on license file" 1>&2
   140         exit 1
   141     fi
   142     if [ ! -w $prefix/etc/openpkg/license.d ]; then
   143         echo "openpkg:license:ERROR: unable to store signature -- permission problems?" 1>&2
   144         exit 1
   145     fi
   146     cat $src >$prefix/etc/openpkg/license.d/$id || exit $?
   147     chown $musr:$mgrp $prefix/etc/openpkg/license.d/$id >/dev/null 2>&1 || true
   148 }
   150 #   uninstall license
   151 do_uninstall () {
   152     if [ $# -ne 1 ]; then
   153         echo "openpkg:license:USAGE: openpkg license uninstall <id>" 1>&2
   154         exit 1
   155     fi
   156     id="$1"
   157     if [ ! -f $prefix/etc/openpkg/license.d/$id ]; then
   158         echo "openpkg:license:ERROR: no license under id \"$id\" installed" 1>&2
   159         exit 1
   160     fi
   161     id_active="`cat $prefix/etc/openpkg/license`"
   162     if [ ".$id" = ".$id_active" ]; then
   163         echo "openpkg:license:ERROR: license under id \"$id\" still activated -- activate a different one first" 1>&2
   164         exit 1
   165     fi
   166     rm -f $prefix/etc/openpkg/license.d/$id
   167     if [ $? -ne 0 ]; then
   168         echo "openpkg:license:ERROR: failed to uninstall license" 1>&2
   169         exit 1
   170     fi
   171 }
   173 #   activate license
   174 do_activate () {
   175     if [ $# -ne 1 ]; then
   176         echo "openpkg:license:USAGE: openpkg license activate <id>" 1>&2
   177         exit 1
   178     fi
   179     id="$1"
   180     if [ ! -f $prefix/etc/openpkg/license.d/$id ]; then
   181         echo "openpkg:license:ERROR: no license under id \"$id\" installed" 1>&2
   182         exit 1
   183     fi
   184     id_active="`cat $prefix/etc/openpkg/license`"
   185     if [ ".$id" = ".$id_active" ]; then
   186         echo "openpkg:license:ERROR: license id \"$id\" is already activated" 1>&2
   187         exit 1
   188     fi
   189     echo "$id" >$prefix/etc/openpkg/license
   190     if [ $? -ne 0 ]; then
   191         echo "openpkg:license:ERROR: failed to activate license under id \"$id\"" 1>&2
   192         exit 1
   193     fi
   194 }
   196 #   view license
   197 do_view () {
   198     if [ $# -ne 1 ]; then
   199         echo "openpkg:license:USAGE: openpkg license view <id>" 1>&2
   200         exit 1
   201     fi
   202     id="$1"
   203     if [ ! -f $prefix/etc/openpkg/license.d/$id ]; then
   204         echo "openpkg:license:ERROR: no license under id \"$id\" installed" 1>&2
   205         exit 1
   206     fi
   207     viewer=""
   208     for name in less more cat; do
   209         for dir in `echo $PATH | sed -e 's;:; ;g'`; do
   210             if [ -x $dir/$name ]; then
   211                 viewer="$dir/$name"
   212                 break
   213             fi
   214         done
   215         if [ ".$viewer" != . ]; then
   216             break
   217         fi
   218     done
   219     eval $viewer $prefix/etc/openpkg/license.d/$id
   220 }
   222 #   list license
   223 do_list () {
   224     if [ $# -ne 0 ]; then
   225         echo "openpkg:license:USAGE: openpkg license list" 1>&2
   226         exit 1
   227     fi
   228     id_active="`cat $prefix/etc/openpkg/license`"
   229     for file in `cd / && find $prefix/etc/openpkg/license.d -type f -print | sort`; do
   230         id=`echo $file | sed -e "s;^$prefix/etc/openpkg/license.d/;;"`
   231         status="-"
   232         if [ ".$id" = ".$id_active" ]; then
   233             status="+"
   234         fi
   235         echo . | awk '{ printf("%s %s\n", status, id); }' id="$id" status="$status"
   236     done
   237 }
   239 #   list currently activate license
   240 do_active () {
   241     if [ $# -ne 0 ]; then
   242         echo "openpkg:license:USAGE: openpkg license active" 1>&2
   243         exit 1
   244     fi
   245     cat $prefix/etc/openpkg/license
   246 }
   248 #   sanity-check license
   249 do_sanity () {
   250     if [ $# -ne 0 ]; then
   251         echo "openpkg:license:USAGE: openpkg license sanity" 1>&2
   252         exit 1
   253     fi
   254     id="`cat $prefix/etc/openpkg/license`"
   255     if [ ! -f $prefix/etc/openpkg/license.d/$id ]; then
   256         echo "openpkg:license:ERROR: active license with id \"$id\" does not exist" 1>&2
   257         exit 1
   258     fi
   259     if ! valid_signature $prefix/etc/openpkg/license.d/$id; then
   260         echo "openpkg:license:ERROR: invalid signature on active license" 1>&2
   261         exit 1
   262     fi
   263 }
   265 #   Command Line Dispatching
   266 cmd="$1"
   267 shift
   268 case "$cmd" in
   269     help      ) do_help      ${1+"$@"} ;;
   270     install   ) do_install   ${1+"$@"} ;;
   271     update    ) do_update    ${1+"$@"} ;;
   272     uninstall ) do_uninstall ${1+"$@"} ;;
   273     activate  ) do_activate  ${1+"$@"} ;;
   274     view      ) do_view      ${1+"$@"} ;;
   275     list      ) do_list      ${1+"$@"} ;;
   276     active    ) do_active    ${1+"$@"} ;;
   277     sanity    ) do_sanity    ${1+"$@"} ;;
   278     ""        ) echo "openpkg:license:ERROR: no command given (use \"help\" for usage)" 1>&2; exit 1 ;;
   279     *         ) echo "openpkg:license:ERROR: invalid command \"$cmd\" (use \"help\" for usage)" 1>&2; exit 1 ;;
   280 esac

mercurial