fix on nc invocation and several fixes on regexp

master
Hiltjo Posthuma 7 years ago committed by KatolaZ
parent 3ed2653b14
commit e86c8c70ad
  1. 53
      gosher
  2. 55
      gosher.8

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

@ -4,61 +4,46 @@
.Os Linux .Os Linux
.Sh NAME .Sh NAME
.Nm gosher .Nm gosher
.Nd a Gopher server in a shell script .Nd a Gopher server in a shell script
.Sh SYNOPSIS .Sh SYNOPSIS
.Nm gosher .Nm gosher
.Op Ar GOPHERDIR Op Ar HOSTNAME Op Ar PORT .Op Ar GOPHERDIR Op Ar HOSTNAME Op Ar PORT
.Sh DESCRIPTION .Sh DESCRIPTION
.Pp .Nm
.Ic gosher
is a server for the Gopher protocol (RFC 1436) in a POSIX shell is a server for the Gopher protocol (RFC 1436) in a POSIX shell
script. script.
.Pp .Pp
Gopher is a textual protocol for information exchange over the Internet, Gopher is a textual protocol for information exchange over the Internet,
that provides a unified hierarchical menu-based interface. The Gopher that provides a unified hierarchical menu-based interface.
protocol has assigned port 70 by IANA. The Gopher protocol has assigned port 70 by IANA.
.Pp .Pp
.Ic gosher .Nm
uses uses
.Em netcat(1) .Xr nc 1
to listen for incoming connections on the specified to listen for incoming connections on the specified
.Ar HOSTNAME .Ar HOSTNAME
(defaults to 'localhost' if not specified) and (defaults to 'localhost' if not specified) and
.Ar PORT .Ar PORT
(defaults to 70 if (defaults to 70 if
.Ar PORT .Ar PORT
is not specified) and makes available all the files under the folder is not specified) and makes available all the files under the folder
.Ar GOPHERDIR .Ar GOPHERDIR
(defaults to the current directory if not specified). (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
.Sh SEE ALSO .Sh SEE ALSO
lynx(1), gopher(1), sacc(1), clic(1) .Xr clic 1 ,
.\" .Sh CONFORMING TO .Xr gopher 1 ,
.\" .Sh HISTORY .Xr lynx 1 ,
.Xr sacc 1 ,
.Sh AUTHORS .Sh AUTHORS
.Ic gosher .Nm
is Free Software, distributed under the terms of the GNU is Free Software, distributed under the terms of the GNU
General Public License (GPL), version 3 of the License. General Public License (GPL), version 3 of the License.
.Pp .Pp
.Ic gosher .Nm
is copyleft (c) 2018 by is copyleft (c) 2018 by
.An Vincenzo (KatolaZ) Nicosia <katolaz@freaknet.org> .An Vincenzo (KatolaZ) Nicosia Aq Mt katolaz@freaknet.org
.Sh SECURITY CONSIDERATIONS .Sh SECURITY CONSIDERATIONS
It is recommended to run It is recommended to run
.Ic gosher .Nm
under chroot(8). under
.Xr chroot 8 .

Loading…
Cancel
Save