#!/bin/bash (source) # Some utility functions for common tests function Make_Directory() { mkdir -p "$@" ;} function Exists() { [ -e "$1" ] ;} function Is_Alias() { alias "$1" &> /dev/null ;} function Is_Real_Directory() { [ -d "$1" -a ! -L "$1" ] ;} function Is_Real_Empty_Directory() { Is_Real_Directory "$1" && Is_Empty_Directory "$1" ;} function Is_Real_Nonempty_Directory() { Is_Real_Directory "$1" && Is_Nonempty_Directory "$1" ;} function Is_Directory() { [ -d "$1" ] ;} function Is_Executable() { [ -x "$1" ] ;} function Is_File() { [ -f "$1" ] ;} function Is_Process() { pidof "$1" &> /dev/null ;} function Is_Readable() { [ -e "$1" -a -r "$1" ] ;} function Is_Writable() { [ \( -e "$1" -a -w "$1" \) -o \( ! -e "$1" -a -w "$(dirname "$1")" \) ] ;} function Is_Link() { [ -L "$1" -a -e "$1" ] ;} function Is_Broken_Link() { [ -L "$1" -a ! -e "$1" ] ;} function Executable_Exists_In_Path() { [ -n "$(type -p "${1}" 2>/dev/null)" ] ;} # These functions provide basic tests to see if a file qualifies as a GoboLinux # package or recipe. These test could (should) be extended to use 'file' to find # out mime type and/or list the contents of the archive and look for specific # files like 'Recipe' or 'Resources/Dependencies' function Is_GoboLinux_Package() { Is_File "$1" && \ test -z "$(echo "$(basename "$1")" | \ sed -r 's/^[0-9A-Za-z_.]+--[0-9A-Za-z_.]+(-r[0-9]*)?--(.+).tar.bz2/\2/' | \ sed -r '/i686/d;/ppc/d;/PPC/d')" } function Is_GoboLinux_Recipe() { Is_File "$1" && \ -z "$(echo "$(basename "$1")" | \ sed -r 's/^[0-9A-Za-z_.]+--[0-9A-Za-z_.]+(-r[0-9]*)?--[rR]ecipe.tar.bz2/d' )" } function Is_Empty_Directory() { local anyfiles anyfiles=$(ls -A "$1" 2> /dev/null) ! test "$anyfiles" } function Is_Nonempty_Directory() { local anyfiles anyfiles=$(ls -A "$1" 2> /dev/null) [ -d "$1" -a -n "$anyfiles" ] } function Assert_Dir() { local dir=$1 if ! Is_Directory "$dir" then Quiet mkdir -p "$dir" || \ $sudo_exec mkdir -p "$dir" if [ "$?" != "0" ] then Die "Unable to create $dir" fi $sudo_exec chmod a+rx "$dir" fi } # Returns the given filename if it does not exist, # or a variation if it does. # (not perfect due to multiuser/multitasking) function Unique_Name() { if [ ! -e "$1" ] then echo "$1" return fi local i=1 while [ -e "$1-$i" ] do i=$[i+1] done echo "$1-$i" } # Returns a new temporary file function Temporary_File() { mktemp "$goboTemp/$scriptName.temp.XXXXXXXX" } # Returns a new temporary directory function Temporary_Dir() { mktemp -d "$goboTemp/$scriptName.temp.XXXXXXXX" } # Returns line $1 of file $2 function Read_Line() { head -n "$1" "$2" | tail -n 1 } # Count lines of file $1 avoiding the "is-last-line-empty" problem function Count_Lines() { cat -n "$1" | tail -1 | cut -b 1-7 } function Is_Extension() { Parameters "$@" extension filename [ "`basename "$filename" "$extension"`" != "`basename "$filename"`" ] } #detsch, 23/08/2004 function Is_URL() { [ "`echo "$1" | cut -b-5`" = "http:" -o \ "`echo "$1" | cut -b-6`" = "https:" -o \ "`echo "$1" | cut -b-4`" = "ftp:" ] } #calica 14/09/2004 - function Line_Match() { Exists "$1" || return $? cat "$1" | grep -E -e "^.*$2.*$" 2>&1 >/dev/null [ "$?" = "0" ] } #calica 09/11/2005 Move GPG_gobo_keyring to Functions/File GPG_gobo_keyring="${goboPrograms}/Scripts/Current/Data/gpg/goboring.gpg" #calica 11-10-2004 - These routines handle package/recipe signing and verification function Gen_FileHash() { Parameters "$@" file_prefix hashfile local dname="$(dirname "$hashfile")" $sudo_exec rm -f -- "$hashfile" # Validate password $sudo_validate Assert_Dir "$dname" # Below may seem odd. We can't just Verify_Superuser script because # gpg must run as normal user. Quiet pushd "$file_prefix" $sudo_exec env SUDO_OK=1 ListProgramFiles --files --links "$file_prefix" | $sudo_exec python -c """# import sys, os, os.path, hashlib def sha1sum_string(data): return hashlib.sha1(data).hexdigest() def sha1sum_file(filename): f = open(filename, 'r') data = f.read() f.close() return sha1sum_string(data) print 'GoboLinux FileHash, version 2.1, SHA-1' for line in sys.stdin: line = line.split('\n')[0] if line == 'Resources/FileHash': continue if os.path.islink(line): sum = sha1sum_string(os.readlink(line)) else: sum = sha1sum_file(line) print '%s %s' % (sum, line) """ | $sudo_exec tee -a "$hashfile" >&$verboseFD Quiet popd } # Verifies gpg is installed and user has private key function Can_Sign() { gpg=`(which gpg2 || which gpg) 2> /dev/null` [ "$gpg" ] || { Log_Verbose "gpg not found." return 1 } secretkeys=$($gpg -n --list-secret-keys 2>/dev/null) exitcode=$? [ -n "${secretkeys}" -a ${exitcode} -eq 0 ] } function Sign_File() { Parameters "$@" file sig if echo "$file" | grep -q "\$.*$" then file=`echo $file | sed 's,\$,\\$,g'` fi gpg=`(which gpg2 || which gpg) 2> /dev/null` output="`$gpg ${GPG_opts} --armor --output - --detach-sig "$file"`" result="$?" Quiet $sudo_exec rm -f "$sig" if [ $result -eq 0 ] then printf '%s' "$output" | Quiet $sudo_exec tee "$sig" else Log_Verbose "$output" fi return $result } function Check_FileSig() { Parameters "$@" file sig gpgv=`(which gpgv2 || which gpgv) 2> /dev/null` [ "$gpgv" ] || { Log_Verbose "gpgv not found." return 1 } $gpgv ${GPG_opts} ${GPG_gobo_keyring:+--keyring "${GPG_gobo_keyring}"} ${TrustedKeyring:+--keyring "${TrustedKeyring}"} ${UserKeyring:+--keyring "${UserKeyring}"} ${ExtraKeyring:+--keyring "${ExtraKeyring}"} "$sig" "$file" } function Check_FileHash() { ### python check_filehashes CHANGELOG # 10/10/2004 - [calica] First version Parameters "$@" prefix hash_file cleanhashfilename=$(echo "$hash_file" | sed 's,'"$prefix"'\/*,,g') local rtn Quiet pushd "$prefix" $sudo_validate || sudo_exec="" $sudo_exec ListProgramFiles --files --links "$prefix" | grep -v "$cleanhashfilename" | $sudo_exec python -c """# import re, os, string, os.path, sys import hashlib def sha1sum_string(data): return hashlib.sha1(data).hexdigest() def sha1sum_file(filename): f = open(filename, 'r') data = f.read() f.close() return sha1sum_string(data) hashfile='$hash_file' file_checksums={} errors=0 hashes = file(hashfile, 'r') lines = hashes.readlines() version = 1 if lines[0] == 'GoboLinux FileHash, version 2.1, SHA-1\n': version = 2.1 lines = lines[1:] elif lines[0] == 'GoboLinux FileHash, version 2.0, SHA-1\n': version = 2 lines = lines[1:] elif lines[0].startswith('GoboLinux FileHash'): format = lines[0].split('\n')[0] print 'Unsupported FileHash format (%s), please upgrade Scripts.' % format sys.exit(-1) for line in lines: fields = re.split('\s+', line) key = fields[1] for i in xrange(2, len(fields)): if len(fields[i]) > 0: key = key +' ' +fields[i] file_checksums[key]=fields[0] # Walk file tree for line in sys.stdin: try: line = line.split('\n')[0] good_sum = file_checksums[line] if not good_sum: raise Exception if version == 1: test_sum = sha1sum_file(line) elif version >= 2: if os.path.islink(line): test_sum = sha1sum_string(os.readlink(line)) else: test_sum = sha1sum_file(line) del file_checksums[line] if good_sum != test_sum: print line, 'has been modified!' errors+=1 except: if version == 2.1 or not os.path.islink(line): print line, 'not found in hashfile' errors+=1 for missing in file_checksums: print missing, 'is missing' errors+=1 sys.exit(errors) """ rtn=$? Quiet popd return $rtn } function Get_Size() { Parameters "$@" file ls -l "$file" | awk '{print $5}' } function Get_MD5() { Parameters "$@" file md5sum "$file" | awk '{print $1}' }