michael@13: #!@l_prefix@/lib/openpkg/bash michael@13: ## michael@13: ## uuid -- OpenPKG UUID Update Utility michael@13: ## Copyright (c) 2000-2007 OpenPKG Foundation e.V. michael@13: ## Copyright (c) 2000-2007 Ralf S. Engelschall michael@13: ## michael@13: ## Permission to use, copy, modify, and distribute this software for michael@13: ## any purpose with or without fee is hereby granted, provided that michael@13: ## the above copyright notice and this permission notice appear in all michael@13: ## copies. michael@13: ## michael@13: ## THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED michael@13: ## WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF michael@13: ## MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. michael@13: ## IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR michael@13: ## CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@13: ## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@13: ## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF michael@13: ## USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND michael@13: ## ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, michael@13: ## OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT michael@13: ## OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF michael@13: ## SUCH DAMAGE. michael@13: ## michael@13: michael@13: # configuration michael@13: prefix="@l_prefix@" michael@13: musr="@l_musr@" michael@13: mgrp="@l_mgrp@" michael@13: prog_rpm="$prefix/libexec/openpkg/rpm" michael@13: prog_shtool="$prefix/lib/openpkg/shtool" michael@13: prog_miniperl="$prefix/lib/openpkg/miniperl" michael@13: prog_uuid="$prefix/lib/openpkg/uuid" michael@13: file_uuid="$prefix/etc/openpkg/uuid" michael@13: michael@13: # minimum command line parsing michael@13: opt_v=no michael@13: opt_m=no michael@13: while [ 1 ]; do michael@13: case "$1" in michael@13: -v | --verbose ) opt_v=yes; shift ;; michael@13: -m | --multicast ) opt_m=yes; shift ;; michael@13: * ) break ;; michael@13: esac michael@13: done michael@13: michael@13: # special "Nil UUID" michael@13: UUID_NIL="00000000-0000-0000-0000-000000000000" michael@13: michael@13: # Query Dynamic Information michael@13: do_query () { michael@13: # query RPM information michael@13: query=":" michael@13: for var in \ michael@13: l_openpkg_release \ michael@13: l_prefix \ michael@13: l_susr l_suid l_sgrp l_sgid \ michael@13: l_musr l_muid l_mgrp l_mgid \ michael@13: l_rusr l_ruid l_rgrp l_rgid \ michael@13: l_nusr l_nuid l_ngrp l_ngid; do michael@13: query="$query; `echo $var | sed -e 's;^l_;Q_;'`=\"%{$var}\"" michael@13: done michael@13: eval `$prog_rpm --eval "$query" 2>/dev/null` michael@13: michael@13: # query OS information michael@13: Q_platform=`$prog_shtool platform --type=binary 2>/dev/null |\ michael@13: sed -e 's;^\([^-][^-]*-[^.-][^.-]*\)\..*$;\1;'` michael@13: if [ ".$Q_platform" = . ]; then michael@13: Q_platform="unknown" michael@13: fi michael@13: Q_discriminator=`$prog_uuid -v1 | $prog_uuid -d -- - | awk '/node:.*global unicast/ { printf("mac:%s", $2); }'` michael@13: if [ ".$Q_discriminator" = . ]; then michael@13: Q_discriminator=`$prog_miniperl -e 'if (-f "/etc/openpkg") { printf("inode:%s", (stat("/etc/openpkg"))[1]); }' 2>/dev/null` michael@13: if [ ".$Q_discriminator" = . ]; then michael@13: Q_discriminator=`$prog_shtool echo -e 'fqdn:%h%d' 2>/dev/null` michael@13: fi michael@13: fi michael@13: } michael@13: michael@13: # Load Configuration michael@13: do_load () { michael@13: # start with reasonable defaults michael@13: UUID_REGISTRY="$UUID_NIL" michael@13: UUID_INSTANCE="$UUID_NIL" michael@13: UUID_PLATFORM="$UUID_NIL" michael@13: michael@13: # load configuration (and override defaults) michael@13: if [ -r $file_uuid ]; then michael@13: if [ ".$opt_v" = .yes ]; then michael@13: echo "uuid: loading UUID configuration file ($file_uuid)" michael@13: fi michael@13: . $file_uuid michael@13: fi michael@13: michael@13: # remember whether something was changed michael@13: changed=no michael@13: } michael@13: michael@13: # Save Configuration michael@13: do_save () { michael@13: if [ ".$changed" = .yes ]; then michael@13: # save configuration michael@13: if [ ".$opt_v" = .yes ]; then michael@13: echo "uuid: saving UUID configuration file ($file_uuid)" michael@13: fi michael@13: if [ -f $file_uuid ] && [ ! -w $file_uuid ]; then michael@13: echo "uuid:ERROR: cannot write to $file_uuid" 1>&2 michael@13: exit 1 michael@13: fi michael@13: ( echo "UUID_REGISTRY=\"$UUID_REGISTRY\"" michael@13: echo "UUID_INSTANCE=\"$UUID_INSTANCE\"" michael@13: echo "UUID_PLATFORM=\"$UUID_PLATFORM\"" michael@13: ) >$file_uuid.new || exit $? michael@13: n=`egrep 'UUID_(REGISTRY|INSTANCE|PLATFORM)="[0-9a-f-]*"' \ michael@13: $file_uuid.new | wc -l | awk '{ print $1; }'` michael@13: if [ ".$n" != .3 ]; then michael@13: echo "uuid:ERROR: failed to update $file_uuid" 1>&2 michael@13: rm -f $file_uuid.new michael@13: exit 1 michael@13: fi michael@13: cp $file_uuid.new $file_uuid || exit $? michael@13: rm -f $file_uuid.new || true michael@13: chown $musr:$mgrp $file_uuid >/dev/null 2>&1 || true michael@13: chmod 644 $file_uuid >/dev/null 2>&1 || true michael@13: michael@13: # remember that no more changes exist michael@13: changed=no michael@13: fi michael@13: } michael@13: michael@13: # Reset UUIDs michael@13: do_reset () { michael@13: # reset all UUIDs to the "Nil UUID" michael@13: if [ ".$UUID_REGISTRY" != ".$UUID_NIL" ]; then michael@13: UUID_REGISTRY="$UUID_NIL" michael@13: changed=yes michael@13: fi michael@13: if [ ".$UUID_INSTANCE" != ".$UUID_NIL" ]; then michael@13: UUID_INSTANCE="$UUID_NIL" michael@13: changed=yes michael@13: fi michael@13: if [ ".$UUID_PLATFORM" != ".$UUID_NIL" ]; then michael@13: UUID_PLATFORM="$UUID_NIL" michael@13: changed=yes michael@13: fi michael@13: } michael@13: michael@13: # Update UUIDs michael@13: do_update () { michael@13: # update registry UUID michael@13: if [ ".$UUID_REGISTRY" = ".$UUID_NIL" -o ".$UUID_REGISTRY" = . ]; then michael@13: opt="" michael@13: if [ ".$opt_m" = .yes ]; then michael@13: opt="-m" michael@13: fi michael@13: if [ ".$opt_v" = .yes ]; then michael@13: echo "uuid: calculating OpenPKG Registry UUID (UUID_REGISTRY):" michael@13: echo "uuid: << $prog_uuid -v1 $opt" michael@13: fi michael@13: uuid=`$prog_uuid -v1 $opt` michael@13: if [ ".$UUID_REGISTRY" != ".$uuid" -a ".$uuid" != . ]; then michael@13: UUID_REGISTRY="$uuid" michael@13: if [ ".$opt_v" = .yes ]; then michael@13: echo "uuid: >> $UUID_REGISTRY (NOT REPEATABLE)" michael@13: fi michael@13: changed=yes michael@13: fi michael@13: fi michael@13: michael@13: # update instance UUID michael@13: name="${Q_openpkg_release}" michael@13: name="$name:${Q_prefix}" michael@13: name="$name:${Q_susr}:${Q_suid}:${Q_sgrp}:${Q_sgid}" michael@13: name="$name:${Q_musr}:${Q_muid}:${Q_mgrp}:${Q_mgid}" michael@13: name="$name:${Q_rusr}:${Q_ruid}:${Q_rgrp}:${Q_rgid}" michael@13: name="$name:${Q_nusr}:${Q_nuid}:${Q_ngrp}:${Q_ngid}" michael@13: if [ ".$opt_v" = .yes ]; then michael@13: echo "uuid: querying OpenPKG Instance OID" michael@13: echo "uuid: << $prog_rpm --eval '%{l_openpkg_oid_instance}'" michael@13: fi michael@13: OID_NS_INSTANCE="`$prog_rpm --eval '%{l_openpkg_oid_instance}'`" michael@13: if [ ".$opt_v" = .yes ]; then michael@13: echo "uuid: >> $OID_NS_INSTANCE" michael@13: fi michael@13: if [ ".$opt_v" = .yes ]; then michael@13: echo "uuid: calculating OpenPKG Instance Namespace UUID" michael@13: echo "uuid: << $prog_uuid -v3 ns:OID \"$OID_NS_INSTANCE\"" michael@13: fi michael@13: UUID_NS_INSTANCE=`$prog_uuid -v3 ns:OID "$OID_NS_INSTANCE"` michael@13: if [ ".$opt_v" = .yes ]; then michael@13: echo "uuid: >> $UUID_NS_INSTANCE" michael@13: fi michael@13: if [ ".$opt_v" = .yes ]; then michael@13: echo "uuid: calculating OpenPKG Instance UUID (UUID_INSTANCE):" michael@13: echo "uuid: << $prog_uuid -v3 \"$UUID_NS_INSTANCE\" \"$name\"" michael@13: fi michael@13: uuid=`$prog_uuid -v3 "$UUID_NS_INSTANCE" "$name"` michael@13: if [ ".$opt_v" = .yes ]; then michael@13: echo "uuid: >> $uuid" michael@13: fi michael@13: if [ ".$UUID_INSTANCE" != ".$uuid" -a ".$uuid" != . ]; then michael@13: UUID_INSTANCE="$uuid" michael@13: changed=yes michael@13: fi michael@13: michael@13: # update platform UUID michael@13: name="${Q_platform}" michael@13: name="$name:${Q_discriminator}" michael@13: if [ ".$opt_v" = .yes ]; then michael@13: echo "uuid: querying OpenPKG Platform OID" michael@13: echo "uuid: << $prog_rpm --eval '%{l_openpkg_oid_platform}'" michael@13: fi michael@13: OID_NS_PLATFORM="`$prog_rpm --eval '%{l_openpkg_oid_platform}'`" michael@13: if [ ".$opt_v" = .yes ]; then michael@13: echo "uuid: >> $OID_NS_PLATFORM" michael@13: fi michael@13: if [ ".$opt_v" = .yes ]; then michael@13: echo "uuid: calculating OpenPKG Platform Namespace UUID" michael@13: echo "uuid: << $prog_uuid -v3 ns:OID \"$OID_NS_PLATFORM\"" michael@13: fi michael@13: UUID_NS_PLATFORM=`$prog_uuid -v3 ns:OID "$OID_NS_PLATFORM"` michael@13: if [ ".$opt_v" = .yes ]; then michael@13: echo "uuid: >> $UUID_NS_PLATFORM" michael@13: fi michael@13: if [ ".$opt_v" = .yes ]; then michael@13: echo "uuid: calculating OpenPKG Platform UUID (UUID_PLATFORM):" michael@13: echo "uuid: << $prog_uuid -v3 \"$UUID_NS_PLATFORM\" \"$name\"" michael@13: fi michael@13: uuid=`$prog_uuid -v3 "$UUID_NS_PLATFORM" "$name"` michael@13: if [ ".$opt_v" = .yes ]; then michael@13: echo "uuid: >> $uuid" michael@13: fi michael@13: if [ ".$UUID_PLATFORM" != ".$uuid" -a ".$uuid" != . ]; then michael@13: UUID_PLATFORM="$uuid" michael@13: changed=yes michael@13: fi michael@13: } michael@13: michael@13: # Info about input and resulting UUIDs michael@13: do_info () { michael@13: echo "OpenPKG Summary of Identification Information" michael@13: echo "=============================================" michael@13: echo "" michael@13: echo "OpenPKG Registry" michael@13: time=""; clock=""; node="" michael@13: eval `$prog_uuid -d "$UUID_REGISTRY" 2>/dev/null |\ michael@13: sed -e 's/^/X/' \ michael@13: -e 's/X.*time: *\(.*\)$/time="\1";/' \ michael@13: -e 's/X.*clock: *\(.*\)$/clock="\1";/' \ michael@13: -e 's/X.*node: *\(.*\)$/node="\1";/' \ michael@13: -e 's/^X.*//'` michael@13: echo " System Time: ${time:-unknown}" michael@13: echo " System Clock Sequence: ${clock:-unknown}" michael@13: echo " System Node Address: ${node:-unknown}" michael@13: echo " UUID_REGISTRY: $UUID_REGISTRY" michael@13: echo "" michael@13: echo "OpenPKG Instance" michael@13: echo " Release: ${Q_openpkg_release}" michael@13: echo " Prefix: ${Q_prefix}" michael@13: echo " Super Account: ${Q_susr}(${Q_suid}):${Q_sgrp}(${Q_sgid})" michael@13: echo " Management Account: ${Q_musr}(${Q_muid}):${Q_mgrp}(${Q_mgid})" michael@13: echo " Restricted Account: ${Q_rusr}(${Q_ruid}):${Q_rgrp}(${Q_rgid})" michael@13: echo " Nonprivileged Account: ${Q_nusr}(${Q_nuid}):${Q_ngrp}(${Q_ngid})" michael@13: echo " UUID_INSTANCE: $UUID_INSTANCE" michael@13: echo "" michael@13: echo "OpenPKG Platform" michael@13: echo " Platform Id: $Q_platform" michael@13: echo " Discriminator: $Q_discriminator" michael@13: echo " UUID_PLATFORM: $UUID_PLATFORM" michael@13: echo "" michael@13: echo "(run \"$0 --verbose update\" to reproduce the UUID generation)" michael@13: } michael@13: michael@13: # Command Line Dispatching michael@13: cmd="$1" michael@13: shift michael@13: case "$cmd" in michael@13: reset ) michael@13: do_load; do_reset; do_save michael@13: ;; michael@13: update ) michael@13: do_query; do_load; do_update; do_save michael@13: ;; michael@13: info ) michael@13: do_query; do_load; do_info michael@13: ;; michael@13: * ) michael@13: echo "uuid:ERROR: invalid command \"$cmd\"" 1>&2 michael@13: exit 1 michael@13: ;; michael@13: esac michael@13: