From 3b62f18221c92ef75cfb80b506ea1ce6d02e3abb Mon Sep 17 00:00:00 2001 From: CyberLeo Date: Tue, 6 Jan 2009 07:42:51 -0600 Subject: [PATCH] Initial import --- flag | 176 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 176 insertions(+) create mode 100755 flag diff --git a/flag b/flag new file mode 100755 index 0000000..63f44dd --- /dev/null +++ b/flag @@ -0,0 +1,176 @@ +#!/bin/sh + +#set -x + +# Places for flags: +# $HOME/.flag-cache <- Autogenerated flag (refreshed when hostname changes) +# /etc/flag <- Site admin's flag +# $HOME/.flag <- Local user's flag +# Check them in that order. + +hash_string() { + # Hash the passed string into 8 hex characters + [ -z "${crc32_bin}" ] && find_crc32_bin + if [ -z "${crc32_bin}" ] + then + echo "No crc32 algo found!" >&2 + echo "deadbeef" + else + echo -n "${*}" | eval "${crc32_bin}" | awk '{print $1}' + fi +} + +find_crc32_bin() { + crc32="" + for bin in crc32sum crc32sum-linux crc32sum-freebsd + do + bin="$(which "${bin}" 2>/dev/null)" + if [ -x "${bin}" ] + then + export crc32_bin="${bin}" + if [ "$(hash_string "meow" 2>/dev/null)" = "8a106afe" ] + then + crc32="${bin}" + break + fi + fi + done + + if [ -z "${crc32}" ] + then + for bin in ruby php perl + do + bin="$(which "${bin}" 2>/dev/null)" + if [ -x "${bin}" ] + then + case "${bin}" in + */ruby) bin="${bin} -e 'require \"zlib\"; printf(\"%08x\n\", Zlib.crc32(STDIN.readlines.join))'" ;; + */php) bin="${bin} -r 'printf(\"%08x\n\", crc32(file_get_contents(\"/dev/stdin\")));'" ;; + */perl) bin="${bin} -e 'use String::CRC32; printf(\"%08x\n\", crc32(*STDIN));'" ;; + esac + export crc32_bin="${bin}" + if [ "$(hash_string "meow" 2>/dev/null)" = "8a106afe" ] + then + crc32="${bin}" + break + fi + fi + done + fi + if [ -n "${crc32}" ] + then + export crc32_bin="${crc32}" + else + echo "Failed to locate usable crc32 algo" + unset crc32_bin + return 1 + fi +} + +find_crc32_bin + +flagcache(){ + myhost="$(/bin/hostname -f)" + cache="${HOME}/.flag-cache" + if [ -f "${cache}" ] + then + flag_hex="$(awk '/^'${myhost}'/{print $2}' "${cache}")" + fi + if [ -z "${flag_hex}" ] + then + flag_hex="$(hash_string "${myhost}")" + echo -e "${myhost}\t${flag_hex}" >> "${cache}" + fi +} + +if [ -f "${HOME}/.flag" ] +then + flag_hex="$(cat "${HOME}/.flag")" +fi + +if [ -z "${flag_hex}" -a -f "/etc/flag" ] +then + flag_hex="$(cat "/etc/flag")" +fi + +if [ -z "${flag_hex}" ] +then + flagcache +fi + +# Now we definitely have flag_hex +firstchar() { + eval $(echo -en "${data}" | sed -e 's/^\(.\)\(.*\)$/char="\1" data="\2"/g') +} + +hexdec() { + if [ -z "${1}" ] + then + echo "0" + return + fi + echo $(( 0x${1} + 0 )) +} + +flag() { + if [ -z "${1}" ] + then + echo "Usage: flag 'hexcode' [symbol] [shellmode]" + echo "Generates an ANSI color flag using the specified hex codes" + echo "Uses the optional symbol (or ' ') to draw the flag" + echo "shellmode encapsulates all nonprintable codes in \[\] to" + echo " give the shell hints on how long the string is, for proper" + echo " wrapping of command prompts." + return + fi + data="${1}" + sym="${2}" + shm="${3}" + [ -z "${sym}" ] && sym=" " + [ -n "${shm}" ] && open="\[" shut="\]" + while [ -n "${data}" ] + do + firstchar + ord=$(hexdec "${char}") + bright="0" + [ "$(( ${ord} & 8 ))" -gt 0 ] && bright="1" + fore="3$(( ${ord} & 7 ))" + back="4$(( ${ord} & 7 ))" + printf "${open}\033[%s;%s;%s;7m${shut}%s" "${bright}" "${fore}" "${back}" "${sym}" + done + printf "${open}\033[0m${shut}" +} + +pebkac() { + echo "Usage: $(basename "${0}") [-s #] [-e]" + echo "Produces a neat little ansi colour 'flag' based off" + echo " a hash of the machine's hostname (or settable via" + echo " ~/.flag or /etc/flag) which can uniquely visually" + echo " identify a machine, at a glance. Useful for placing" + echo " into /etc/issue or your bash prompt, so that you" + echo " don't send stupid commands to the wrong machine." + echo " " + echo " -s # Specify which character should be used to" + echo " render the flag" + echo " -e Turn on bash encapsulation mode, to provide" + echo " hints to Bash on what is or is not printable" + echo " so it can wrap your commands appropriately" + echo " " + echo "This machine's flag is as such: ($(flag "${flag_hex}"))" + exit 1 +} + +symbol=" " +shellmode="" + +while [ -n "${1}" ] +do + case "${1}" in + -s) shift; symbol="${1}" ;; + -e) shellmode="yes" ;; + *) pebkac ;; + esac + shift +done +flag "${flag_hex}" "${symbol}" "${shellmode}" + -- 2.42.0