summaryrefslogtreecommitdiff
path: root/nocompile/libexec/gopherd.sh
blob: 78509dc4b7d2b9c236fb8cf0697e42ec3143630f (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/bin/bash
#usage: $1 is the directory to be served over gopher.
# If a file is executable it is going to be used as a program.
# If there's a .header file in any directory it'll get printed
# before the directory listing.
# gopherd.sh tries to guess the hostname to use for people accessing the 
# server (since there isn't a header for it like in HTTP).
# this requires rDNS records to be set correctly.
# Or you can just comment out that code and put a value at
# "export hostname" down there.
export PATH=$PATH:/usr/local/bin
read -t 10 req
logger -t gopher "$(/usr/local/libexec/peerip|head -n1): ${req}"
base="$1"
arg=$(echo "$req" | tr -d '\r' | cut -f2)
#req=$(echo "$req" | unescape | tr -d '\r' | cut -f1)
req=$(echo "$req" | tr -d '\r' | sed 's/\/*$//' | cut -f1)
cd ${base}
realpath="$(normalpath "${base}${req}")"
if grep -v "^${base}" <<< "${realpath}" > /dev/null;then
 echo -e 'i'${base}'\tasdf\tasdf\tasdf\r'
 echo -e 'i'${realpath}'\tadsf\tasdf\tasdf\r'
 echo -e 'ierror!!! cant find base in realpath\tasdf\tasdf\tasdf\r'
 exit 1
fi
#myIP=$(hop0 $(/usr/local/libexec/peerip | head -n1) | cut -d% -f1)
myIP=$(/usr/local/libexec/sockip | head -n1 | cut -d% -f1)
hostname=$(rdns ${myIP})
if [ ! "${hostname}" ]; then
 hostname=${myIP}
else
 if strstr "${myIP}" ":";then
  if [ $(gethostbyname $hostname 6) != ${myIP} ];then
   printf ""
#   echo "fucked off #1"
#   echo "$myIP != $(gethostbyname $hostname 6)"
#   logger -t gopher "hostname (${hostname}) and IP (${myIP}) aren't matching up.";
#   exit 2
#jimmy cracked corn but nobody gives a shit.
  fi
 else
  if [ $(gethostbyname $hostname) != ${myIP} ];then
   echo "fucked off #2"
   logger -t gopher "hostname (${hostname}) and IP (${myIP}) aren't matching up.";
   exit 2
  fi
 fi
fi
export hostname
export port=70
type=$(file -L "${realpath}" | cut -d: -f2-)
if strstr "$type" "directory";then
 if [ -e "${realpath}/.header" ];then
  awk '{print "i"$0"\tfake\t(NULL)\t0\r"}' "${realpath}/.header"
 fi
 ls "${realpath}" | while read i;do
  stype=$(file -L "${realpath}/${i}" | cut -d: -f2-)
  if strstr "$stype" "directory"; then
   printf "1%s\t%s\t%s\t%s\r\n" "${i}" "${req}/${i}" $hostname $port
  else
   if [ "_$(stat -L "${realpath}/${i}" | cut '-d ' -f3 | grep -v l | tr 'x' '\n' | wc -l | tr -d ' ')" = "_4" ] ;then
    printf "7%s\t%s\t%s\t%s\r\n" "${i}" "${req}/${i}" $hostname $port
   else
    printf "0%s\t%s\t%s\t%s\r\n" "${i}" "${req}/${i}" $hostname $port
   fi
  fi
 done
 printf ".\r\n"
else
#this shit is ugly
 numX="$(stat -L "${realpath}" | cut '-d ' -f3 | grep -v l | tr 'x' '\n' | wc -l | tr -d ' ')"
 if [ "_$numX" = "_3" ];then
  #an executable that outputs text and pretends to be a type 0.
  "${realpath}"
 elif [ "_$numX" = "_4" ];then
  #a type 7
  "${realpath}" "$arg" | sed "s/\$/"`printf "\r"`"/g"
 else
  #output the text of the file
  cat "${realpath}"
 fi
fi