#!/bin/bash # Usage: # ./is-perl-object /path/to/file # # Returns a zero status code and prints the object's identity if the file can # be identified as a self-modifying Perl script, and returns a status code of # 1 otherwise. # Identity retrieval is optimized, but will query the Perl object directly if # the optimistic lookup fails. If the Perl object is unidentified, this may # cause an ID to be generated for that object, in which case it will rewrite # itself if possible. # To prevent this script from querying the object, you can use the --no-query # or -n arguments. if [[ $1 == '-n' || $1 == '--no-query' ]]; then no_query=$1 shift fi magic=$(head -c64 "$1" | tail -n1 | awk '{print $2}') if [[ $magic == '99aeabc9ec7fe80b1b39f5e53dc7e49e' ]]; then identity_line="$(grep "^meta::data('permanent-identity'" "$1")" if [[ $? == 0 ]]; then echo "$identity_line" | tail -c36 | head -c32 echo exit 0 elif [[ ! "$no_query" ]]; then "./$1" identity exit 0 else exit 2 fi else exit 1 fi