openpkg/uuid.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
parent 13
cb59d6afeb61
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@"
    29 prog_rpm="$prefix/libexec/openpkg/rpm"
    30 prog_shtool="$prefix/lib/openpkg/shtool"
    31 prog_miniperl="$prefix/lib/openpkg/miniperl"
    32 prog_uuid="$prefix/lib/openpkg/uuid"
    33 file_uuid="$prefix/etc/openpkg/uuid"
    35 #   minimum command line parsing
    36 opt_v=no
    37 opt_m=no
    38 while [ 1 ]; do
    39     case "$1" in
    40         -v | --verbose   ) opt_v=yes; shift ;;
    41         -m | --multicast ) opt_m=yes; shift ;;
    42         * ) break ;;
    43     esac
    44 done
    46 #   special "Nil UUID"
    47 UUID_NIL="00000000-0000-0000-0000-000000000000"
    49 #   Query Dynamic Information
    50 do_query () {
    51     #   query RPM information
    52     query=":"
    53     for var in \
    54         l_openpkg_release \
    55         l_prefix \
    56         l_susr l_suid l_sgrp l_sgid \
    57         l_musr l_muid l_mgrp l_mgid \
    58         l_rusr l_ruid l_rgrp l_rgid \
    59         l_nusr l_nuid l_ngrp l_ngid; do
    60         query="$query; `echo $var | sed -e 's;^l_;Q_;'`=\"%{$var}\""
    61     done
    62     eval `$prog_rpm --eval "$query" 2>/dev/null`
    64     #   query OS information
    65     Q_platform=`$prog_shtool platform --type=binary 2>/dev/null |\
    66         sed -e 's;^\([^-][^-]*-[^.-][^.-]*\)\..*$;\1;'`
    67     if [ ".$Q_platform" = . ]; then
    68         Q_platform="unknown"
    69     fi
    70     Q_discriminator=`$prog_uuid -v1 | $prog_uuid -d -- - | awk '/node:.*global unicast/ { printf("mac:%s", $2); }'`
    71     if [ ".$Q_discriminator" = . ]; then
    72         Q_discriminator=`$prog_miniperl -e 'if (-f "/etc/openpkg") { printf("inode:%s", (stat("/etc/openpkg"))[1]); }' 2>/dev/null`
    73         if [ ".$Q_discriminator" = . ]; then
    74             Q_discriminator=`$prog_shtool echo -e 'fqdn:%h%d' 2>/dev/null`
    75         fi
    76     fi
    77 }
    79 #  Load Configuration
    80 do_load () {
    81     #   start with reasonable defaults
    82     UUID_REGISTRY="$UUID_NIL"
    83     UUID_INSTANCE="$UUID_NIL"
    84     UUID_PLATFORM="$UUID_NIL"
    86     #   load configuration (and override defaults)
    87     if [ -r $file_uuid ]; then
    88         if [ ".$opt_v" = .yes ]; then
    89             echo "uuid: loading UUID configuration file ($file_uuid)"
    90         fi
    91         . $file_uuid
    92     fi
    94     #   remember whether something was changed
    95     changed=no
    96 }
    98 #  Save Configuration
    99 do_save () {
   100     if [ ".$changed" = .yes ]; then
   101         #   save configuration
   102         if [ ".$opt_v" = .yes ]; then
   103             echo "uuid: saving UUID configuration file ($file_uuid)"
   104         fi
   105         if [ -f $file_uuid ] && [ ! -w $file_uuid ]; then
   106             echo "uuid:ERROR: cannot write to $file_uuid" 1>&2
   107             exit 1
   108         fi
   109         ( echo "UUID_REGISTRY=\"$UUID_REGISTRY\""
   110           echo "UUID_INSTANCE=\"$UUID_INSTANCE\""
   111           echo "UUID_PLATFORM=\"$UUID_PLATFORM\""
   112         ) >$file_uuid.new || exit $?
   113         n=`egrep 'UUID_(REGISTRY|INSTANCE|PLATFORM)="[0-9a-f-]*"' \
   114            $file_uuid.new | wc -l | awk '{ print $1; }'`
   115         if [ ".$n" != .3 ]; then
   116             echo "uuid:ERROR: failed to update $file_uuid" 1>&2
   117             rm -f $file_uuid.new
   118             exit 1
   119         fi
   120         cp $file_uuid.new $file_uuid || exit $?
   121         rm -f $file_uuid.new || true
   122         chown $musr:$mgrp $file_uuid >/dev/null 2>&1 || true
   123         chmod 644 $file_uuid >/dev/null 2>&1 || true
   125         #   remember that no more changes exist
   126         changed=no
   127     fi
   128 }
   130 #  Reset UUIDs
   131 do_reset () {
   132     #   reset all UUIDs to the "Nil UUID"
   133     if [ ".$UUID_REGISTRY" != ".$UUID_NIL" ]; then
   134         UUID_REGISTRY="$UUID_NIL"
   135         changed=yes
   136     fi
   137     if [ ".$UUID_INSTANCE" != ".$UUID_NIL" ]; then
   138         UUID_INSTANCE="$UUID_NIL"
   139         changed=yes
   140     fi
   141     if [ ".$UUID_PLATFORM" != ".$UUID_NIL" ]; then
   142         UUID_PLATFORM="$UUID_NIL"
   143         changed=yes
   144     fi
   145 }
   147 #   Update UUIDs
   148 do_update () {
   149     #   update registry UUID
   150     if [ ".$UUID_REGISTRY" = ".$UUID_NIL" -o ".$UUID_REGISTRY" = . ]; then
   151         opt=""
   152         if [ ".$opt_m" = .yes ]; then
   153             opt="-m"
   154         fi
   155         if [ ".$opt_v" = .yes ]; then
   156             echo "uuid: calculating OpenPKG Registry UUID (UUID_REGISTRY):"
   157             echo "uuid: << $prog_uuid -v1 $opt"
   158         fi
   159         uuid=`$prog_uuid -v1 $opt`
   160         if [ ".$UUID_REGISTRY" != ".$uuid" -a ".$uuid" != . ]; then
   161             UUID_REGISTRY="$uuid"
   162             if [ ".$opt_v" = .yes ]; then
   163                 echo "uuid: >> $UUID_REGISTRY (NOT REPEATABLE)"
   164             fi
   165             changed=yes
   166         fi
   167     fi
   169     #   update instance UUID
   170     name="${Q_openpkg_release}"
   171     name="$name:${Q_prefix}"
   172     name="$name:${Q_susr}:${Q_suid}:${Q_sgrp}:${Q_sgid}"
   173     name="$name:${Q_musr}:${Q_muid}:${Q_mgrp}:${Q_mgid}"
   174     name="$name:${Q_rusr}:${Q_ruid}:${Q_rgrp}:${Q_rgid}"
   175     name="$name:${Q_nusr}:${Q_nuid}:${Q_ngrp}:${Q_ngid}"
   176     if [ ".$opt_v" = .yes ]; then
   177         echo "uuid: querying OpenPKG Instance OID"
   178         echo "uuid: << $prog_rpm --eval '%{l_openpkg_oid_instance}'"
   179     fi
   180     OID_NS_INSTANCE="`$prog_rpm --eval '%{l_openpkg_oid_instance}'`"
   181     if [ ".$opt_v" = .yes ]; then
   182         echo "uuid: >> $OID_NS_INSTANCE"
   183     fi
   184     if [ ".$opt_v" = .yes ]; then
   185         echo "uuid: calculating OpenPKG Instance Namespace UUID"
   186         echo "uuid: << $prog_uuid -v3 ns:OID \"$OID_NS_INSTANCE\""
   187     fi
   188     UUID_NS_INSTANCE=`$prog_uuid -v3 ns:OID "$OID_NS_INSTANCE"`
   189     if [ ".$opt_v" = .yes ]; then
   190         echo "uuid: >> $UUID_NS_INSTANCE"
   191     fi
   192     if [ ".$opt_v" = .yes ]; then
   193         echo "uuid: calculating OpenPKG Instance UUID (UUID_INSTANCE):"
   194         echo "uuid: << $prog_uuid -v3 \"$UUID_NS_INSTANCE\" \"$name\""
   195     fi
   196     uuid=`$prog_uuid -v3 "$UUID_NS_INSTANCE" "$name"`
   197     if [ ".$opt_v" = .yes ]; then
   198         echo "uuid: >> $uuid"
   199     fi
   200     if [ ".$UUID_INSTANCE" != ".$uuid" -a ".$uuid" != . ]; then
   201         UUID_INSTANCE="$uuid"
   202         changed=yes
   203     fi
   205     #   update platform UUID
   206     name="${Q_platform}"
   207     name="$name:${Q_discriminator}"
   208     if [ ".$opt_v" = .yes ]; then
   209         echo "uuid: querying OpenPKG Platform OID"
   210         echo "uuid: << $prog_rpm --eval '%{l_openpkg_oid_platform}'"
   211     fi
   212     OID_NS_PLATFORM="`$prog_rpm --eval '%{l_openpkg_oid_platform}'`"
   213     if [ ".$opt_v" = .yes ]; then
   214         echo "uuid: >> $OID_NS_PLATFORM"
   215     fi
   216     if [ ".$opt_v" = .yes ]; then
   217         echo "uuid: calculating OpenPKG Platform Namespace UUID"
   218         echo "uuid: << $prog_uuid -v3 ns:OID \"$OID_NS_PLATFORM\""
   219     fi
   220     UUID_NS_PLATFORM=`$prog_uuid -v3 ns:OID "$OID_NS_PLATFORM"`
   221     if [ ".$opt_v" = .yes ]; then
   222         echo "uuid: >> $UUID_NS_PLATFORM"
   223     fi
   224     if [ ".$opt_v" = .yes ]; then
   225         echo "uuid: calculating OpenPKG Platform UUID (UUID_PLATFORM):"
   226         echo "uuid: << $prog_uuid -v3 \"$UUID_NS_PLATFORM\" \"$name\""
   227     fi
   228     uuid=`$prog_uuid -v3 "$UUID_NS_PLATFORM" "$name"`
   229     if [ ".$opt_v" = .yes ]; then
   230         echo "uuid: >> $uuid"
   231     fi
   232     if [ ".$UUID_PLATFORM" != ".$uuid" -a ".$uuid" != . ]; then
   233         UUID_PLATFORM="$uuid"
   234         changed=yes
   235     fi
   236 }
   238 #   Info about input and resulting UUIDs
   239 do_info () {
   240     echo "OpenPKG Summary of Identification Information"
   241     echo "============================================="
   242     echo ""
   243     echo "OpenPKG Registry"
   244     time=""; clock=""; node=""
   245     eval `$prog_uuid -d "$UUID_REGISTRY" 2>/dev/null |\
   246           sed -e 's/^/X/' \
   247               -e 's/X.*time:  *\(.*\)$/time="\1";/' \
   248               -e 's/X.*clock:  *\(.*\)$/clock="\1";/' \
   249               -e 's/X.*node:  *\(.*\)$/node="\1";/' \
   250               -e 's/^X.*//'`
   251     echo "    System Time:           ${time:-unknown}"
   252     echo "    System Clock Sequence: ${clock:-unknown}"
   253     echo "    System Node Address:   ${node:-unknown}"
   254     echo "    UUID_REGISTRY:         $UUID_REGISTRY"
   255     echo ""
   256     echo "OpenPKG Instance"
   257     echo "    Release:               ${Q_openpkg_release}"
   258     echo "    Prefix:                ${Q_prefix}"
   259     echo "    Super Account:         ${Q_susr}(${Q_suid}):${Q_sgrp}(${Q_sgid})"
   260     echo "    Management Account:    ${Q_musr}(${Q_muid}):${Q_mgrp}(${Q_mgid})"
   261     echo "    Restricted Account:    ${Q_rusr}(${Q_ruid}):${Q_rgrp}(${Q_rgid})"
   262     echo "    Nonprivileged Account: ${Q_nusr}(${Q_nuid}):${Q_ngrp}(${Q_ngid})"
   263     echo "    UUID_INSTANCE:         $UUID_INSTANCE"
   264     echo ""
   265     echo "OpenPKG Platform"
   266     echo "    Platform Id:           $Q_platform"
   267     echo "    Discriminator:         $Q_discriminator"
   268     echo "    UUID_PLATFORM:         $UUID_PLATFORM"
   269     echo ""
   270     echo "(run \"$0 --verbose update\" to reproduce the UUID generation)"
   271 }
   273 #   Command Line Dispatching
   274 cmd="$1"
   275 shift
   276 case "$cmd" in
   277     reset )
   278         do_load; do_reset; do_save
   279         ;;
   280     update )
   281         do_query; do_load; do_update; do_save
   282         ;;
   283     info )
   284         do_query; do_load; do_info
   285         ;;
   286     * )
   287         echo "uuid:ERROR: invalid command \"$cmd\"" 1>&2
   288         exit 1
   289         ;;
   290 esac

mercurial