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