diff options
Diffstat (limited to 'gosher')
-rwxr-xr-x | gosher | 53 |
1 files changed, 26 insertions, 27 deletions
@@ -4,12 +4,12 @@ # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or (at # your option) any later version. -# +# # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. -# +# # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. # @@ -21,7 +21,7 @@ ## ## $ ./gosher [<GOPHERDIR> [<HOSTNAME> [<PORT>]]] ## -## If GOPHERDIR is not specified, "./" is assumed. +## If GOPHERDIR is not specified, "./" is assumed. ## If HOSTNAME is not specified, "localhost" is used. ## If PORT is not specified, the default port "70" is used. ## @@ -45,8 +45,8 @@ ### ncat (from nmap) ##NETCAT="ncat" ## -### Openbsd netcat -NETCAT="nc.openbsd" +### OpenBSD netcat +NETCAT="nc" ## ## STYLE: The way in which netcat will talk to gosher_serve @@ -65,8 +65,7 @@ DEBUG= [ -n "$DEBUG" ] && { - set -e - set -x + set -e -x } @@ -77,7 +76,7 @@ cleanup(){ } MYNAME=$(basename "$0") -MYDIR=$(dirname "$(realpath "$0")") +MYDIR=$(dirname "$(readlink -f "$0")") NETCAT=$(which "$NETCAT") if [ -z "${MYNAME#gosher}" ]; then @@ -85,16 +84,16 @@ if [ -z "${MYNAME#gosher}" ]; then GOPHERDIR=${1:-"./"} HOSTNAME=${2:-"localhost"} PORT=${3:-70} - + [ ! -f "${NETCAT}" ] || [ ! -x "${NETCAT}" ] && { echo "Wrong NETCAT -- Exiting" >&2 exit 2 } - + if [ -f "${MYDIR}/gosher_serve" ] || [ -h "${MYDIR}/gosher_serve" ]; then GOSHER_SERVE="${MYDIR}/gosher_serve" trap cleanup 0 HUP INT TRAP TERM QUIT - + INF="${IPREFIX}$$" [ "$STYLE" = "pipe" ] && { mkfifo -m 600 "$INF" @@ -103,7 +102,7 @@ if [ -z "${MYNAME#gosher}" ]; then # shellcheck disable=SC2094 ${GOSHER_SERVE} "${GOPHERDIR}" \ "${HOSTNAME}" "${PORT}" <"$INF" |\ - ${NETCAT} -vvvvv -l -p "${PORT}" >"$INF" + ${NETCAT} -vvvvv -l "${HOSTNAME}" "${PORT}" >"$INF" done rm -f $INF exit 0 @@ -161,16 +160,16 @@ serve_index(){ IDX=$1 IFS=' ' - + echo "[$(date +%Y-%m-%d\ %H:%M:%S)|GPH|${IDX}|\"\"]" >&2 while read -r line; do - rline=$(echo "$line" | sed -r -e 's/\r//g') + rline=$(echo "$line" | tr -d '\r') case "$rline" in '['*) - echo "$rline" | sed -r -e 's/\[//g;s/\]//g;s/\|/\t/g;s/\t//;s/$/\r/g' + echo "$rline" | sed -r -e 's/\[//g;s/\]//g;s/\|/ /g;s/ //;s/$/\r/g' ;; t*) - echo "$rline" | cut -c 2- + echo "$rline" | cut -c 2- ;; *) echo "$line" @@ -187,7 +186,7 @@ serve_index(){ ## function serve_redirect(){ url=$1 - + echo "[$(date +%Y-%m-%d\ %H:%M:%S)|REDIRECT|${url}|\"\"]" >&2 cat<< EOF <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> @@ -225,12 +224,12 @@ serve_cgi(){ SERVER_SOFTWARE="gosher" ####X_GOPHER_SEARCH= search (See above.) export GATEWAY_INTERFACE PATH_INFO PATH_TRANSLATED QUERY_STRING - export REMOTE_ADDR REMOTE_HOST REQUEST_METHOD SCRIPT_NAME + export REMOTE_ADDR REMOTE_HOST REQUEST_METHOD SCRIPT_NAME export SERVER_NAME SERVER_PORT SERVER_PROTOCOL SERVER_SOFTWARE ${GOPHERDIR}${script_name} "" "${query_string}" "${HOSTNAME}" "${PORT}" 2>&1 exec 1>&- exec 2>&- - exit $? + exit $? } @@ -240,7 +239,7 @@ PORT=${3:-"70"} read -r selector -selector=$(echo "$selector" | sed -r 's:\$.*::g;s:\r::g' ) +selector=$(echo "$selector" | tr -d '\r' ) [ -n "$DEBUG" ] && { echo "iGOPHERDIR: ${GOPHERDIR}" @@ -258,10 +257,10 @@ case $selector in script_name=$(echo "$selector" | cut -d "?" -f 1) query_string=$(echo "$selector" | cut -d "?" -f 2) [ "${script_name}" = "${query_string}" ] && query_string="" - RP1=$(realpath "${GOPHERDIR}""${script_name}" || "") + RP1=$(readlink -f "${GOPHERDIR}""${script_name}" || "") # shellcheck disable=SC2181 [ $? -eq 0 ] || invalid_selector "${selector}" - RP2=$(realpath "${GOPHERDIR}")"${script_name}" + RP2=$(readlink -f "${GOPHERDIR}")"${script_name}" RP2=$(echo "${RP2}" | sed -r 's/\/+/\//g') # shellcheck disable=SC2181 [ $? -eq 0 ] || invalid_selector "${selector}" @@ -269,7 +268,7 @@ case $selector in echo "iRP1: ${RP1}" echo "iRP2: ${RP2}" } - + [ "${RP1}" = "${RP2}" ] && { [ -x "${RP1}" ] && { serve_cgi "${script_name}" "${query_string}" @@ -279,20 +278,20 @@ case $selector in ;; /?*|"") ## it's a regular selector - RP1=$(realpath "${GOPHERDIR}"/"${selector}" || "") + RP1=$(readlink -f "${GOPHERDIR}"/"${selector}" || "") # shellcheck disable=SC2181 [ $? -eq 0 ] || invalid_selector "$selector" - RP2=$(realpath "${GOPHERDIR}")"${selector}" + RP2=$(readlink -f "${GOPHERDIR}")"${selector}" # shellcheck disable=SC2181 [ $? -eq 0 ] || invalid_selector "$selector" [ -n "$DEBUG" ] && { echo "iRP1: ${RP1}" echo "iRP2: ${RP2}" } - + if [ "${RP1}" = "${RP2}" ]; then if [ -f "${RP1}" ]; then - if [ -n "$(echo "${RP1}" | sed -n '/\.gph$/p')" ]; then + if [ -n "$(echo "${RP1}" | sed -n '/\.gph$/p')" ]; then serve_index "${RP1}" else serve_selector "${RP1}" |