summaryrefslogtreecommitdiff
path: root/nocompile/libexec/gopherd.sh
blob: 57b35549d024746e0cee9381effb3b6ca8a0e8f3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/bin/bash
export PATH=$PATH:/usr/local/bin
read -t 10 req
base="$1"
arg=$(echo "$req" | tr -d '\r' | cut -f2)
req=$(echo "$req" | tr -d '\r' | cut -f1)
realpath="$(realpath "${base}${req}")"
if grep -v "^${base}" <<< "${realpath}" > /dev/null;then
 echo ${base}
 echo ${realpath}
 echo 'error!!! cant find base in realpath'
 exit 1
fi
myIP=$(hop0 $(/usr/local/libexec/peerip | head -n1))
hostname=$(rdns ${myIP})
if [ ! "${hostname}" ]; then
 hostname=${myIP}
else
 if strstr "${myIP}" ":";then
  if [ $(dig -t AAAA +short $hostname) != ${myIP} ];then
   logger "hostname (${hostname}) and IP (${myIP}) aren't matching up.";
   exit 2
  fi
 else
  if [ $(dig -t A +short $hostname) != ${myIP} ];then
   logger "hostname (${hostname}) and IP (${myIP}) aren't matching up.";
   exit 2
  fi
 fi
fi
export hostname
export port=70
type=$(file "${realpath}" | cut -d: -f2-)
if strstr "$type" "directory";then
 if [ -e "${realpath}/.header" ];then
  cat "${realpath}/.header" | while read -r l;do
   printf "i%s\t%s\t%s\t%s\r\n" "$l" "fake" "(NULL)" "0"
  done
 fi
 ls "${realpath}" | while read i;do
  stype=$(file "${realpath}/${i}" | cut -d: -f2-)
  if strstr "$stype" "directory"; then
   printf "1%s\t%s\t%s\t%s\r\n" "${req}/${i}" "${req}/${i}" $hostname $port
  else
   if stat "${realpath}/${i}" | cut '-d ' -f3 | grep x >/dev/null;then
    printf "7%s\t%s\t%s\t%s\r\n" "${req}/${i}" "${req}/${i}" $hostname $port
   else
    printf "0%s\t%s\t%s\t%s\r\n" "${req}/${i}" "${req}/${i}" $hostname $port
   fi
  fi
 done
 printf ".\r\n"
else
 if stat "${realpath}" | cut '-d ' -f3 | grep x >/dev/null;then
  "${realpath}" $arg | sed "s/\$/"`printf "\r"`"/g"
 else
  cat "${realpath}"
 fi
fi