#----------------------------------------------------------------------------
# /www/srv/admin/cgi-helper
#
# Creation: 2005-08-12 hh
# Last Update: $Id$
#----------------------------------------------------------------------------
#----------------------------------------------------------------------------
# do some useful things
#----------------------------------------------------------------------------
#set_debug=yes
# parse variables
cgi_helper="yes"
eval "`proccgi $*`"
: ${set_debug:=$FORM_debug}
# Write debug info to debug.log
case $set_debug in
yes)
exec 2>/srv/www/admin/debug.log
set -x
;;
*)
exec 2>/dev/null
;;
esac
. /var/run/mini_httpd.conf
myname=`basename $0`
: ${cginame:=`basename $0 .cgi`}
: ${lang:="$HTTPD_GUI_LANG"}
: ${showmenu:=$FORM_showmenu}
# get language files
if [ -f /srv/www/lang/main.$lang ]
then
. /srv/www/lang/main.$lang
else
. /srv/www/lang/main.en
fi
if [ -f /srv/www/lang/$cginame.$lang ]
then
. /srv/www/lang/$cginame.$lang
else
[ -f /srv/www/lang/$cginame.en ] && . /srv/www/lang/$cginame.en
fi
#----------------------------------------------------------------------------
# Security function
#----------------------------------------------------------------------------
user_has_rights ()
{ # returns true if the user has access
[ -z "$1" ] && return 0
if grep ^$REMOTE_USER= /etc/httpd/rights | cut -d= -f2 | \
grep -vq -e "^all$" \
-e "\( \|^\)$1:all\( \|$\)" \
-e "\( \|^\)$1[^ ]*[:,]$2\( \|,\|$\)"
then
return 1
else
return 0
fi
}
check_rights ()
{
: ${SEC_REALM:=$1}
: ${SEC_ACTION:=$2}
if [ -z "$SEC_REALM" -o -z "$SEC_ACTION" ]
then
show_html_header "$_MN_accdenied"
show_error "$_MN_seclib" "$_MN_nopar"
show_html_footer
# Exit the CGI-Script
exit 1
fi
case $REMOTE_USER in
"") ;;
*)
if ! user_has_rights $SEC_REALM $SEC_ACTION
then
show_html_header "$_MN_accdenied"
show_error "$_MN_accdenied" "$_MN_noright
\
$_MN_user: \"$REMOTE_USER\"
\
$_MN_realm: \"$SEC_REALM\"
\
$_MN_secaction: \"$SEC_ACTION\""
show_html_footer
# Exit the CGI-Script
exit 1
fi
;;
esac
}
#----------------------------------------------------------------------------
# some useful functions
#----------------------------------------------------------------------------
reload ()
{ # Reload site
echo "Location: $myname"
echo
}
http_header ()
{ # Output Header
case "$1" in
""|html)
cat <<-EOF
Content-Type: text/html; charset=iso-8859-1
Cache-Control: max-age=1, s-maxage=1, no-cache, must-revalidate, no-store
Pragma: no-cache
EOF
echo "Expires: `date -Ru`"
echo
;;
htmlstatic)
cat <<-EOF
Content-Type: text/html; charset=iso-8859-1
Cache-control: max-age=3600
EOF
;;
download)
eval $2
: ${ctype:=application/download}
: ${filename:=file.txt}
echo "Content-Type: $ctype"
echo "Content-Disposition: attachment; filename=$filename"
echo
;;
esac
}
create_menu ()
{
# read the menu file and create a menu
menucache="/tmp/menu_${REMOTE_USER}`echo $menufile | sed 's#/#_#g'`"
if ! [ -f "$menucache" ]
then
sec_close=
cursec_print=
{
while read type prio link desc right
do
# Include the cgi-specific Language-File if available
cginame=`echo $link | sed 's/.*\///;s/\.cgi$//'`
if [ "$cginame" != '-' ]; then
if [ -f /srv/www/lang/$cginame.$lang ]
then
. /srv/www/lang/$cginame.$lang
else
[ -f /srv/www/lang/$cginame.en ] && . /srv/www/lang/$cginame.en
fi
fi
eval desc=\"$desc\"
case "$type" in
t)
case "$desc" in
Opt) cursec=$_MT_opt ;;
*) cursec=$desc ;;
esac
cursec_print=yes
;;
e)
if user_has_rights "$right" "view"
then
if [ -n "$cursec_print" ]
then
echo $sec_close'
" cat /srv/www/admin/debug.log | htmlspecialchars echo "" ;; esac # restore stdout exec 1>&3 # do the output http_header # with the redirection we are able to do a little cleaning cat /srv/www/include/header.inc /tmp/content_$$.html /srv/www/include/footer.inc | substitute_markers meta css title topnav subtitle date version menu comment_start comment_end $marker | xhtml_cleaning rm -f /tmp/content_$$.html } substitute_markers () { sedtring= for _marker in $* do # we have to escape double quotes, ampersand, dollar substitute=`eval echo '$'${_marker} | sed 's/"/\\\\\"/g; s/&/\\\&/g; s/\\\$/\\\\\$/g'` sedstring="${sedstring}s°###$_marker###°$substitute°g; " done eval sed \"$sedstring\" } xhtml_cleaning () { # do a very basic xhtm cleaning, remove unused markers # todo strtolower of tags #img,br,hr,meta,link,base,area,input,param,col # HTTPD_GUI_XHTML_CLEANING is set on boottime by rc430.httpd eval sed $HTTPD_GUI_XHTML_CLEANING -e 's/\(###\)\([^#]*\)\(###\)//g' } show_tab_header () { echo '
$0[$$]: $1" else echo "$0[$$]: $1" fi fi } cgi_log_errors() { if [ -s "$2" ]; then cgi_log "Errors while rendering '$1'" while read line; do cgi_log "$line" done < $2 fi }