From e86c8c70adb3775f0ae37d081f3679cb95257c99 Mon Sep 17 00:00:00 2001 From: Hiltjo Posthuma Date: Mon, 13 Aug 2018 16:47:20 +0100 Subject: fix on nc invocation and several fixes on regexp --- gosher | 53 ++++++++++++++++++++++++++--------------------------- gosher.8 | 55 ++++++++++++++++++++----------------------------------- 2 files changed, 46 insertions(+), 62 deletions(-) diff --git a/gosher b/gosher index 31fe2d3..37e56dd 100755 --- a/gosher +++ b/gosher @@ -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 . # @@ -21,7 +21,7 @@ ## ## $ ./gosher [ [ []]] ## -## 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 @@ -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}" diff --git a/gosher.8 b/gosher.8 index 45124e0..a3ae9af 100644 --- a/gosher.8 +++ b/gosher.8 @@ -4,61 +4,46 @@ .Os Linux .Sh NAME .Nm gosher -.Nd a Gopher server in a shell script +.Nd a Gopher server in a shell script .Sh SYNOPSIS .Nm gosher .Op Ar GOPHERDIR Op Ar HOSTNAME Op Ar PORT .Sh DESCRIPTION -.Pp -.Ic gosher +.Nm is a server for the Gopher protocol (RFC 1436) in a POSIX shell script. - .Pp Gopher is a textual protocol for information exchange over the Internet, -that provides a unified hierarchical menu-based interface. The Gopher -protocol has assigned port 70 by IANA. - +that provides a unified hierarchical menu-based interface. +The Gopher protocol has assigned port 70 by IANA. .Pp -.Ic gosher +.Nm uses -.Em netcat(1) -to listen for incoming connections on the specified +.Xr nc 1 +to listen for incoming connections on the specified .Ar HOSTNAME -(defaults to 'localhost' if not specified) and +(defaults to 'localhost' if not specified) and .Ar PORT -(defaults to 70 if +(defaults to 70 if .Ar PORT is not specified) and makes available all the files under the folder .Ar GOPHERDIR -(defaults to the current directory if not specified). - - - -.\" This next request is for sections 1, 6, 7 & 8 only -.\".Sh ENVIRONMENT - - -.\".Sh EXAMPLES -.\" This next request is for sections 1, 6, 7 & 8 only -.\" (command return values (to shell) and -.\" fprintf/stderr type diagnostics) - -.\" .Sh DIAGNOSTICS +(defaults to the current directory if not specified). .Sh SEE ALSO -lynx(1), gopher(1), sacc(1), clic(1) -.\" .Sh CONFORMING TO -.\" .Sh HISTORY +.Xr clic 1 , +.Xr gopher 1 , +.Xr lynx 1 , +.Xr sacc 1 , .Sh AUTHORS -.Ic gosher +.Nm is Free Software, distributed under the terms of the GNU General Public License (GPL), version 3 of the License. .Pp -.Ic gosher +.Nm is copyleft (c) 2018 by -.An Vincenzo (KatolaZ) Nicosia - +.An Vincenzo (KatolaZ) Nicosia Aq Mt katolaz@freaknet.org .Sh SECURITY CONSIDERATIONS It is recommended to run -.Ic gosher -under chroot(8). +.Nm +under +.Xr chroot 8 . -- cgit v1.2.3