]> CyberLeo.Net >> Repos - CDN/flag.git/blob - flag
Look for flag in site-local too, for FreeBSD
[CDN/flag.git] / flag
1 #!/bin/sh
2
3 #set -x
4
5 # Places for flags:
6 # $HOME/.flag-cache <- Autogenerated flag (refreshed when hostname changes)
7 # /usr/local/etc/flag <- Site admin's flag (FreeBSD)
8 # /etc/flag <- Site admin's flag
9 # $HOME/.flag <- Local user's flag
10 # Check them in that order.
11
12 # Name available algos as algo_something() and left-align for autodetection
13 # Order the algos by priority: the first one that is correct will be used
14 algo_cksum_linux() { printf '%08x\n' $(cksum) | head -n 1; }
15 algo_cksum_freebsd() { printf '%08x\n' $(cksum -o 3) | head -n 1; }
16 algo_crc32() { crc32 /dev/stdin; }
17 algo_crc32sum() { crc32sum; }
18 algo_crc32sum_linux() { crc32sum-linux; }
19 algo_crc32sum_freebsd() { crc32sum-freebsd; }
20 algo_crc32_gzip() { gzip -c | tail -c 8 | od -An -N 4 -tx4 | tr -dc '0-9a-f\n'; }
21 algo_python() { python -c 'import sys; import zlib; print "%08x\n" % (zlib.crc32("".join(sys.stdin.readlines())) & 0xFFFFFFFF)'; }
22 algo_ruby() { ruby -e 'require "zlib"; printf "%08x\n", Zlib.crc32(STDIN.readlines.join)'; }
23 algo_php() { php -r 'printf("%08x\n", crc32(file_get_contents("/dev/stdin")));'; }
24 algo_perl() { perl -e 'use String::CRC32; printf("%08x\n", crc32(*STDIN));'; }
25
26 find_crc32_algo() {
27   sed -e '/^algo_[^(]*()/!d; s/().*$//' "${0}" | while read algo
28   do
29     if [ "$(printf "meow" | "${algo}" 2>/dev/null)" = "8a106afe" ]
30     then
31       echo "${algo}"
32       break
33     fi
34   done
35 }
36
37 hash_string() {
38   # Hash the passed string into 8 hex characters
39   [ "${crc32_algo}" ] || crc32_algo="$(find_crc32_algo)"
40   if [ -z "${crc32_algo}" ]
41   then
42     echo "No crc32 algo found!" >&2
43     echo "deadbeef"
44     return 1
45   else
46     printf "%s" "${*}" | "${crc32_algo}" 2>/dev/null | awk '{print $1}'
47     return 0
48   fi
49 }
50
51 flagcache(){
52   myhost="$(/bin/hostname -f)"
53   cache="${HOME}/.flag-cache"
54   if [ -f "${cache}" ]
55   then
56     flag_hex="$(awk '/^'${myhost}'/{print $2}' "${cache}")"
57   fi
58   if [ -z "${flag_hex}" ]
59   then
60     flag_hex="$(hash_string "${myhost}")" && printf "%s\t%s\n" "${myhost}" "${flag_hex}" >> "${cache}"
61   fi
62   echo "${flag_hex}"
63 }
64
65 if [ -f "${HOME}/.flag" ]
66 then
67   flag_hex="$(cat "${HOME}/.flag")"
68 fi
69
70 if [ -z "${flag_hex}" ] && [ -f "%%PREFIX%%/etc/flag" ]
71 then
72   flag_hex="$(cat "%%PREFIX%%/etc/flag")"
73 fi
74
75 if [ -z "${flag_hex}" ] && [ -f "/etc/flag" ]
76 then
77   flag_hex="$(cat "/etc/flag")"
78 fi
79
80 if [ -z "${flag_hex}" ]
81 then
82   flag_hex="$(flagcache)"
83 fi
84
85 # Now we definitely have flag_hex
86 firstchar() {
87   char="$(printf '%c' "${data}")"
88   data="${data##${char}}"
89 }
90
91 hexdec() {
92   if [ -z "${1}" ]
93   then
94     echo "0"
95     return
96   fi
97   echo $(( 0x${1} + 0 ))
98 }
99
100 flag() {
101   if [ -z "${1}" ]
102   then
103     echo "Usage: flag 'hexcode' [symbol] [shellmode]" >&2
104     echo "Generates an ANSI color flag using the specified hex codes" >&2
105     echo "Uses the optional symbol (or ' ') to draw the flag" >&2
106     echo "shellmode encapsulates all nonprintable codes in \[\] to" >&2
107     echo " give the shell hints on how long the string is, for proper" >&2
108     echo " wrapping of command prompts." >&2
109     return
110   fi
111   data="${1}"
112   sym="${2}"
113   shm="${3}"
114   [ -n "${shm}" ] && open='\\[' shut='\\]'
115   while [ -n "${data}" ]
116   do
117     firstchar
118     ord=$(hexdec "${char}")
119     bright="0"
120     [ "$(( ${ord} & 8 ))" -gt 0 ] && bright="1;7"
121     colour="$(( ${ord} & 7 ))"
122     printf "${open}\033[%s;3%s;4%sm${shut}%s" "${bright}" "${colour}" "${colour}" "${sym:-${char}}"
123   done
124   printf "${open}\033[0m${shut}"
125 }
126
127 pebkac() {
128    echo "Usage: $(basename "${0}") [-s #] [-e]" >&2
129    echo "Produces a neat little ansi colour 'flag' based off" >&2
130    echo " a hash of the machine's hostname (or settable via" >&2
131    echo " ~/.flag or /etc/flag) which can uniquely visually" >&2
132    echo " identify a machine, at a glance. Useful for placing" >&2
133    echo " into /etc/issue or your bash prompt, so that you" >&2
134    echo " don't send stupid commands to the wrong machine." >&2
135    echo " " >&2
136    echo " -s #   Specify which character should be used to" >&2
137    echo "         render the flag. If empty, use the hex code." >&2
138    echo " -e     Turn on bash encapsulation mode, to provide" >&2
139    echo "         hints to Bash on what is or is not printable" >&2
140    echo "         so it can wrap your commands appropriately" >&2
141    echo "         when used in your prompt" >&2
142    echo " " >&2
143    echo "This machine's flag is as such: ($(flag "${flag_hex}"))" >&2
144    exit 1
145 }
146
147 symbol=""
148 shellmode=""
149
150 while [ -n "${1}" ]
151 do
152   case "${1}" in
153   -s)  shift; symbol="${1}" ;;
154   -e)  shellmode="yes" ;;
155   *)   pebkac ;;
156   esac
157   shift
158 done
159 flag "${flag_hex}" "${symbol}" "${shellmode}"
160