#!/bin/sh # # $Id: advanced-fs.cgi,v 1.7 2007/04/06 03:26:08 syzdek Exp $ # # CGI System Tools # Copyright (C) 2005, 2006, 2007 David M. Syzdek # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 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, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # # advanced-fs.cgi - CGI shell script for exploring a system # # restricts browsing to this web site if test "x${PATH_INFO}" != "x";then DIRMASK=`pwd` DIRMASK=`dirname ${DIRMASK}` DIRMASK=`dirname ${DIRMASK}` echo ${PATH_INFO} |grep "^${DIRMASK}" > /dev/null if test $? -ne 0;then echo "Content-type: text/html" echo "" echo "
"
      echo "*********************** PERMISSION DENIED ***********************"
      echo ""
      echo "   This script was intended for educational purposes. Access"
      echo "   to files or directories out side of this web site have been"
      echo "   restricted."
      echo ""
      echo "*********************** PERMISSION DENIED ***********************"
      echo "
" exit 0 fi fi # +-=-=-=-=-=-=-+ # | | # | functions | # | | # +-=-=-=-=-=-=-+ # provides link to directories dirlink() { if test "x${REAL_SCRIPT_NAME}" = "x";then DOWNLOAD_SCRIPT=${SCRIPT_NAME} else DOWNLOAD_SCRIPT=${REAL_SCRIPT_NAME} fi LIST="" TMP=${1} FILE=`basename ${TMP}` if test -f ${TMP};then LIST="${FILE}${LIST}" else LIST="${FILE}/${LIST}" fi TMP=`dirname ${TMP}` while test "x${TMP}" != "x/";do FILE=`basename ${TMP}` LIST="${FILE}/${LIST}" TMP=`dirname ${TMP}` done LIST="/${LIST}" printf "${LIST}" }; # display group info group_data() { GRP=`echo ${QUERY_STRING}|cut -d\= -f2 |cut -d\& -f1` GROUP=`grep "^${GRP}:" /etc/group` if test "x${GROUP}" = "x";then echo "group not found" return 0 fi ID=`echo ${GROUP} |cut -d: -f3` printf 'Group name: %-27s GID: %s\n' "${GRP}" "${ID}" printf 'Members:' STRLEN=0 for USR in `cut -d: -f1,4 /etc/passwd |grep :${ID}$|cut -d: -f1`;do printf ' %s' ${REQUEST_URI} ${PATH_INFO} ${USR} ${USR} USRLEN=`echo ${USR}|wc -c` STRLEN=`expr ${STRLEN} + ${USRLEN} + 1` if test ${STRLEN} -gt 60;then printf '\n%8s' ' ' STRLEN=0 fi done for USR in `echo ${GROUP} |cut -d: -f4 | sed 's/,/ /g'`;do printf ' %s' ${REQUEST_URI} ${PATH_INFO} ${USR} ${USR} USRLEN=`echo ${USR}|wc -c` STRLEN=`expr ${STRLEN} + ${USRLEN} + 1` if test ${STRLEN} -gt 60;then printf '\n%8s' ' ' STRLEN=0 fi done printf '\n' }; # prints CGI header header() { if test "x${QUERY_STRING}" != "x";then printf "Content-type: text/html\n\n" elif test -d ${PATH_INFO}; then printf "Content-type: text/html\n\n" elif test -f ${PATH_INFO};then if test -r ${PATH_INFO};then HTML=`echo ${PATH_INFO}|sed 's/.*\.html/html/g'` VAR=`cut -b1-2 ${PATH_INFO} |head -1 |sed -n 'l' |cut -b1` if test "x${HTML}" != "xhtml";then if test "x${VAR}" != 'x\';then printf "Content-type: text/plain\n\n" else printf "Content-type: application/octet-stream\n\n" fi else printf "Content-type: text/html\n\n" fi else printf "Content-type: text/plain\n\n" fi cat ${PATH_INFO} 2>&1 exit 0 else printf "Content-type: text/html\n\n" fi }; # locates file locates() { PATTERN=`echo ${QUERY_STRING}|cut -d\= -f2 |cut -d\& -f1` PATTERN=`echo ${PATTERN} |sed -e 's/+/ /g' |sed -e 's/%/\\\x/g'` PATTERN=`printf "${PATTERN}"` printf "
\n" printf " " printf ' ' printf "
\n" if test "x${PATTERN}" != "xlocate";then for FILE in `locate ${PATTERN}|sed 's/ /+/g' 2> /dev/null`;do dirlink ${FILE} printf "
\n" done; fi }; # displays mounts mounts() { #mount printf '\n' for MOUNT in `mount |cut -d\ -f1`;do printf '\n' done printf '
MountFilesystemOptions
' dirlink ${MOUNT} printf '' mount |grep "^${MOUNT} " | awk '{print$3""$4}' printf '
\n' printf "\n\n" printf '\n' for MOUNT in `df -k|grep -v '^Filesystem ' |awk '{print$6}'`;do printf '\n' #printf '\n' done printf '
FilesystemkbytesusedavailcapacityMounted on
' DATA=`df -k |grep " ${MOUNT}$" |awk '{print$1""$2""$3""$4""$5""}'` printf "${DATA}" dirlink "${MOUNT}" printf '
\n' }; # displays current processes processes() { uptime printf '\n\n' ps -ef ps aux -w }; # cleans up request URI request_uri() { #echo "request uri: ${REQUEST_URI}
" #echo "query string: ${QUERY_STRING}
" REQUEST_URI=`echo ${REQUEST_URI}|sed "s,?${QUERY_STRING}$,,g"` REQUEST_URI=`echo ${REQUEST_URI}|sed "s,${PATH_INFO}$,,g"` REQUEST_URI=`echo ${REQUEST_URI}|sed "s,/${DOCUMENT_NAME}$,,g"` REQUEST_URI=`echo ${REQUEST_URI}|sed "s,/$,,g"` REQUEST_URI="${REQUEST_URI}/${DOCUMENT_NAME}" # echo ${REQUEST_URI} }; # display group info #run_command() { # CMD=`echo ${QUERY_STRING}|cut -d\= -f2 |cut -d\& -f1` # CMD=`echo ${CMD} |sed -e 's/+/ /g' |sed -e 's/%/\\\x/g'` # CMD=`printf "${CMD}"` # printf '$ %s\n' "${CMD}" # ${CMD} 2>&1 # printf '$\n' # printf "
\n" # printf " " # printf ' ' # printf "
\n" #}; # display system info system() { uname -a printf '\n\n' "${SCRIPT_URI}" uptime printf "\n\n" # printf "Run command:\n" # printf "
\n" # printf ' ' # printf ' ' # printf "
\n" # printf "\n" # printf 'See mounts: click here\n\n\n' "${SCRIPT_URI}" printf 'Network Interfaces:\n' ifconfig -a printf "\n\n" netstat -r -n }; # display user info user_data() { USR=`echo ${QUERY_STRING}|cut -d\= -f2 |cut -d\& -f1` #PASSWD=`grep "^${USR}:" /etc/passwd` #if test "x${PASSWD}" = "x";then # echo "user not found" # return 0 #fi #ID=`echo ${PASSWD} |cut -d: -f3` #NAME=`echo ${PASSWD} |cut -d: -f5` #DIR=`echo ${PASSWD} |cut -d: -f6` #SHELL=`echo ${PASSWD} |cut -d: -f7` #printf "Login name: %-27s In real life: %s\n" ${USR} "${NAME}" "${NAME}" #printf "Shell: %-32s Directory: %s\n" "${SHELL}" "`dirlink ${DIR}`" #finger -m ${USR} printf "Groups:" for GROUP in `groups ${USR}`;do printf " ${GROUP}" done printf "\n\n" }; # list directory contents list_dir() { if test "x$1" = "x";then DOWNLOAD_SCRIPT=${SCRIPT_NAME} else DOWNLOAD_SCRIPT=$1 fi PATH_INFO=`echo ${PATH_INFO} |sed -e 's/+/ /g' |sed -e 's/%/\\\x/g'` if test ! -d ${PATH_INFO};then printf "directory does not exist" else cd "${PATH_INFO}" 2>&1 for FILE in `ls -a ${PATH_INFO}`;do LISTING=`ls -ld ${PATH_INFO}/${FILE}` #PERMS=`ls -ld ${PATH_INFO}/${FILE}|cut -c1-15` #USER=`ls -ld ${PATH_INFO}/${FILE}|cut -c16-24` #GROUP=`ls -ld ${PATH_INFO}/${FILE}|cut -c25-32` #SIZE=`ls -ld ${PATH_INFO}/${FILE}|cut -c33-54` PERMS=`ls -ld ${PATH_INFO}/${FILE}|awk '{print$1" "$2}'` USER=`ls -ld ${PATH_INFO}/${FILE}|awk '{print$3}'` GROUP=`ls -ld ${PATH_INFO}/${FILE}|awk '{print$4}'` SIZE=`ls -ld ${PATH_INFO}/${FILE}|awk '{print$5}'` DATE=`ls -ld ${PATH_INFO}/${FILE}|awk '{print$6" "$7" "$8}'` printf '%-20s %-8s %8s %8i %-14s' \ "${PERMS}" \ "${REQUEST_URI}" \ "${PATH_INFO}" \ "${USER}" \ "${USER}" \ "${REQUEST_URI}" \ "${PATH_INFO}" \ "${GROUP}" \ "${GROUP}" \ "${SIZE}" \ "${DATE}" \ if test "x${FILE}" = "x.";then printf "${FILE}" elif test "x${FILE}" = "x..";then printf "${FILE}" else if test -f ${FILE};then printf "${FILE}" elif test -L ${FILE};then LINK=`ls -ld ${FILE}|cut -d\> -f2` printf "${FILE} -> ${LINK}" else printf "${FILE}" fi fi printf "\n" done fi }; # +-=-=-=-=-=-=-=-=+ # | | # | main section | # | | # +-=-=-=-=-=-=-=-=+ # sets PATH_INFO if not provided if test "x${PATH_INFO}" = "x";then PATH_INFO=`dirname ${SCRIPT_FILENAME}` fi # checks for source dump if test "x${QUERY_STRING}" = "xsourcedump";then printf 'Content-type: text/plain\n\n' cat $0 exit $? fi # display headers REAL_SCRIPT_NAME=$1 export REAL_SCRIPT_NAME header # prints top of page printf '
\n'

# decides what to do
FUNC=`echo ${QUERY_STRING}|cut -d\= -f1`
if test "x${FUNC}" = "xps";then
   request_uri
   processes
  elif test "x${FUNC}" = "xlocate";then
   request_uri
   locates
  elif test "x${FUNC}" = "xdf";then
   request_uri
   #mounts
   echo "Need to fix bug
Disabled until then." elif test "x${FUNC}" = "xsystem";then request_uri system elif test "x${FUNC}" = "xuser";then request_uri user_data elif test "x${FUNC}" = "xgroup";then request_uri group_data # elif test "x${FUNC}" = "xrun";then # request_uri # run_command else request_uri dirlink ${PATH_INFO} printf '
\n' list_dir $1 fi # print bottom of page printf '
\n' printf '
\n' NODE=`uname -n` printf '%s   ' "${SCRIPT_URI}" "${NODE}" date printf '
\n' # end of script