aboutsummaryrefslogtreecommitdiffstats
path: root/nocompile/libexec/gopherd.sh
blob: 9d3ba5cacf9ba599bb11d13ea4b203d439a52ad0 (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
#!/bin/bash
export PATH=$PATH:/usr/local/bin
#set these two variables if you have your server behind DMZ
read -t 10 req
base="$1"
req=$(echo "$req" | tr -d '\r')
realpath=$(realpath ${base}${req})
if grep -v "^${base}" <<< "${realpath}" > /dev/null;then
 echo ${base}
 echo ${realpath}
 exit 1
fi
myIP=$(hop0 $(/usr/local/libexec/peerip | head -n1))
hostname=$(rdns ${myIP})
if [ ! "${hostname}" ]; then
 hostname=${myIP}
else
 if [ $(dig +short $hostname) != ${myIP} ];then
  logger "hostname (${hostname}) and IP (${myIP}) aren't matching up. >_>";
  exit 2
 fi
fi
type=$(file "${realpath}" | cut -d: -f2-)
if grep directory <<< "$type" 2>&1 > /dev/null; then
 if [ -e "${realpath}/.header" ];then
  cat "${realpath}/.header" | sed "s/^/i&/g" | sed "s/\$/"`printf "\r"`"/g"
 fi
 for i in $(ls "${realpath}");do
  stype=$(file "${realpath}/${i}" | cut -d: -f2-)
  if grep directory <<< "$stype" 2>&1 > /dev/null; then
   printf "1%s\t%s\t%s\t70\r\n" ${req}/${i} ${req}/${i} $hostname
  else
   printf "0%s\t%s\t%s\t70\r\n" ${req}/${i} ${req}/${i} $hostname
  fi
 done
 printf ".\r\n"
else
 if stat "${realpath}" | cut '-d ' -f3 | grep x >/dev/null;then
  "${realpath}"
 else
  cat "${realpath}"
 fi
fi