repo: shell-daemons action: blob revision: path_from: gopherd revision_from: refs/heads/master: path_to: revision_to:
blob of:
/ gopherd
refs/heads/master:/gopherd
#!/bin/bash
unset SUDO_GID
unset SUDO_USER
unset SUDO_COMMAND
unset SUDO_UID
export REMOTE_ADDR="$(/usr/local/libexec/peerip | head -n1)"
export REMOTE_PORT="$(/usr/local/libexec/peerip | tail -n1)"
export SERVER_ADDR="$(/usr/local/libexec/sockip | head -n1)"
export SERVER_PORT="$(/usr/local/libexec/sockip | tail -n1)"
if [ "$SERVER_ADDR" = 21.41.41.3 ];then
export SERVER_NAME=fgc2df7jssqt7adacvhinvwdjmn4gwfp6fqxczxunw4qtb37554yexyd.onion
else
export SERVER_NAME=batou.thebackupbox.net
fi
export REMOTE_ADDR=$(/usr/local/libexec/peerip | head -n1)
export REMOTE_PORT=$(/usr/local/libexec/peerip | tail -n1)
export SERVER_ADDR=$(/usr/local/libexec/sockip | head -n1)
export SERVER_PORT=$(/usr/local/libexec/sockip | tail -n1)
unset NCAT_REMOTE_ADDR
unset NCAT_REMOTE_PORT
unset NCAT_LOCAL_ADDR
unset NCAT_LOCAL_PORT
unset NCAT_PROTO
if read -rt 10 REQUEST_PATH;then
export REQUEST_PATH=$(printf '%s\n' "${REQUEST_PATH}" | tr -d '\r')
logger "gopherd request from $REMOTE_ADDR: $REQUEST_PATH"
export SCRIPT_NAME="$(normalpath "$(uriunescape "/$(printf "%s\n" "$REQUEST_PATH" | tr '?' '\t' | cut -f1)")")" #any attempts at ..ing up won't work because normalpath limits it to /
if fnmatch '/~*' "$SCRIPT_NAME" >/dev/null;then
tilde="$(printf "$SCRIPT_NAME" | cut -d/ -f2)"
user="$(printf "%s\n" "$tilde" | cut -d~ -f2)"
SCRIPT_NAME="$(printf "$SCRIPT_NAME" | cut -d/ -f3-)"
cd "$(getent passwd "$user" | cut -d: -f6)"/public_gopher || exit 0
tilde="${tilde}/"
else
cd /var/gopher
fi
if [ "$(printf "%s\n" "${SCRIPT_NAME}" | cut -d/ -f2)" == "proxy" ];then
proxy_url="$(printf "%s\n" "${SCRIPT_NAME}" | cut -d/ -f3-)"
printf "iThis is a test"
exit
fi
export DOCUMENT_ROOT="$(pwd)"
export SCRIPT_FILE_NAME="$(normalpath "/${DOCUMENT_ROOT}/${SCRIPT_NAME}")"
if [ -d "${SCRIPT_FILE_NAME}" ];then
cat /anarchy/ascii-art/epoch/thebackupbox4 | tr '\n' '\0' | xargs -0 printf "i%s\t.\t.\t.\r\n"
logger "gopherd test"
cd "${SCRIPT_FILE_NAME}"
printf "iThis is epoch's shitty gopher server written in shell script. . . .\r\n"
printf "iTake 2. . . .\r\n"
printf "iur at: %s . . .\r\n" "$(pwd)"
printf "irequest was: %s . . .\r\n" "${REQUEST_PATH}"
printf "1../ %s ${SERVER_NAME} ${SERVER_PORT}\r\n" "$(normalpath "/${REQUEST_PATH}/..")"
find -L -maxdepth 1 -type d -not -path '*/\.*' | tail -n+2 | cut -d/ -f2- | tr '\n' '\0' | xargs -0 -I % printf "1%/ $tilde${SCRIPT_NAME}/% ${SERVER_NAME} ${SERVER_PORT}\r\n" | tr -s '/'
find -L -maxdepth 1 -type f -not -path '*/\.*' | grep -v '^._' | grep -v '\.gmi$' | grep -v '\.html$' | grep -v '\.png$' | grep -v '\.jpg$' | cut -d/ -f2- | tr '\n' '\0' | xargs -0 -I % printf "0% $tilde${SCRIPT_NAME}/% ${SERVER_NAME} ${SERVER_PORT}\r\n" | tr -s '/'
find -L -maxdepth 1 -type f -not -path '*/\.*' -name '*.gmi' | grep -v '^._' | cut -d/ -f2- | cut -d. -f1 | tr '\n' '\0' | xargs -0 -I % printf "1% $tilde${SCRIPT_NAME}/% ${SERVER_NAME} ${SERVER_PORT}\r\n" | tr -s '/'
find -L -maxdepth 1 -type f -not -path '*/\.*' -name '*.html' | grep -v '^._' | cut -d/ -f2- | tr '\n' '\0' | xargs -0 -I % printf "h% $tilde${SCRIPT_NAME}/% ${SERVER_NAME} ${SERVER_PORT}\r\n" | tr -s '/'
find -L -maxdepth 1 -type f -not -path '*/\.*' -name '*.png' | grep -v '^._' | cut -d/ -f2- | tr '\n' '\0' | xargs -0 -I % printf "p% $tilde${SCRIPT_NAME}/% ${SERVER_NAME} ${SERVER_PORT}\r\n" | tr -s '/'
find -L -maxdepth 1 -type f -not -path '*/\.*' -name '*.jpg' | grep -v '^._' | cut -d/ -f2- | tr '\n' '\0' | xargs -0 -I % printf "I% $tilde${SCRIPT_NAME}/% ${SERVER_NAME} ${SERVER_PORT}\r\n" | tr -s '/'
find -L -maxdepth 1 -type f -not -path '*/\.*' -name '?_*' | cut -d/ -f2- | grep -v '^0' | tr '\n' '\0' | xargs -0 -I % printf "% $tilde${SCRIPT_NAME}/% ${SERVER_NAME} ${SERVER_PORT}\r\n" | tr -s '/'
# cat <(find -maxdepth 1 -type f -name '?_*') <(find -maxdepth 1 -type f -name '*.txt') | sort | uniq -d | cut -d/ -f2- | tr '\n' '\0' | xargs -0 -I % printf "0% $tilde${SCRIPT_NAME}/% ${SERVER_NAME} ${SERVER_PORT}\r\n" | tr -s '/'
# cat <(find -maxdepth 1 -type f -name '?_*') <(find -maxdepth 1 -type f -name '*.html') | sort | uniq -d | cut -d/ -f2- | tr '\n' '\0' | xargs -0 -I % printf "h% $tilde${SCRIPT_NAME}/% ${SERVER_NAME} ${SERVER_PORT}\r\n" | tr -s '/'
printf ".\r\n"
elif printf "%s\n" "$REQUEST_PATH" | grep "^/cgi-bin/" 2>&1 >/dev/null;then
cd cgi-bin
export QUERY_STRING="$(printf "$REQUEST_PATH" | tr '?' '\t' | cut -f2- | uriunescape)"
"$SCRIPT_FILE_NAME"
elif [ -f "${SCRIPT_FILE_NAME}.gmi" ];then
cat "${SCRIPT_FILE_NAME}.gmi" | gemini2gophermap.pl
elif [ -f "${SCRIPT_FILE_NAME}" ];then
cat "${SCRIPT_FILE_NAME}"
else
printf "i-d and -f both failed for %s . . .\r\n" "${SCRIPT_FILE_NAME}"
fi
else
printf "iThis is epoch's shitty gopher server written in shell script. . . .\r\n"
printf "ihurry up with the reqest next time. you have 10 seconds. . . .\r\n"
fi