"Fossies" - the Fresh Open Source Software Archive

Member "aif-2.1.1/share/arno-iptables-firewall/aif-job-processor" (16 Sep 2020, 6720 Bytes) of package /linux/privat/aif-2.1.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Bash source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "aif-job-processor": 2.1.0_vs_2.1.1.

    1 #!/bin/sh
    2 
    3 # ------------------------------------------------------------------------------
    4 #                         -= Arno's Iptables Firewall(AIF) =-
    5 #              Single- & multi-homed firewall script with DSL/ADSL support
    6 #
    7 #                           ~ In memory of my dear father ~
    8 #
    9 # (C) Copyright 2001-2020 by Arno van Amersfoort & Lonnie Abelbeck
   10 # Homepage              : https://rocky.eld.leidenuniv.nl/
   11 # Email                 : a r n o v a AT r o c k y DOT e l d DOT l e i d e n u n i v DOT n l
   12 #                         (note: you must remove all spaces and substitute the @ and the .
   13 #                         at the proper locations!)
   14 # ------------------------------------------------------------------------------
   15 # This program is free software; you can redistribute it and/or
   16 # modify it under the terms of the GNU General Public License
   17 # version 2 as published by the Free Software Foundation.
   18 
   19 # This program is distributed in the hope that it will be useful,
   20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
   21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   22 # GNU General Public License for more details.
   23 
   24 # You should have received a copy of the GNU General Public License
   25 # along with this program; if not, write to the Free Software
   26 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
   27 # ------------------------------------------------------------------------------
   28 
   29 # Location of the main configuration file for the firewall
   30 ##########################################################
   31 CONF_FILE=/etc/arno-iptables-firewall/firewall.conf
   32 
   33 # Set environment variables to default
   34 VERBOSE=0
   35 
   36 # Default sleep time (in minutes)
   37 SLEEP_TIME=1
   38 
   39 # Check sanity of environment
   40 sanity_check()
   41 {
   42   if [ -z "$JOBS_FILE" ]; then
   43     echo "** ERROR: Missing JOBS_FILE declaration in environment file!" >&2
   44     return 1
   45   fi
   46 
   47   if [ ! -f "$JOBS_FILE" ]; then
   48     echo "** ERROR: JOBS_FILE($JOBS_FILE) does not exist!" >&2
   49     return 1
   50   fi
   51 
   52   return 0
   53 }
   54 
   55 
   56 idle_wait()
   57 {
   58   local time="$1" chk_file="$2" cnt=0
   59 
   60   while [ $((cnt / 60)) -lt $time ]; do
   61     cnt=$((cnt + 1))
   62     sleep 1
   63 
   64     # Check for file removal
   65     if [ ! -f "$chk_file" ]; then
   66       return 0
   67     fi
   68   done
   69 
   70   return 1
   71 }
   72 
   73 
   74 # (Background) job processor
   75 job_process()
   76 {
   77   local LINE SCRIPT_NAME SCRIPT_SET_TIME SCRIPT_CUR_TIME SCRIPT_PATH READ_JOBS_FILE result retval
   78 
   79   [ $VERBOSE -eq 1 ] && echo "Starting jobs process..."
   80 
   81   # Loop (until JOBS_FILE is deleted)
   82   while true; do
   83     # Enter critical section:
   84     if ! lock_enter "$JOBS_LOCK_NAME"; then
   85       # Lock failed, just keeping trying forever since there's no point in exiting here
   86       [ $VERBOSE -eq 1 ] && echo "Lock failed, (re)trying next loop..."
   87     else
   88       READ_JOBS_FILE="$(cat "$JOBS_FILE" 2>/dev/null)"
   89 
   90       IFS=$EOL
   91       for LINE in $READ_JOBS_FILE; do
   92         SCRIPT_NAME="$(echo "$LINE" |cut -d' ' -f1)"
   93         SCRIPT_SET_TIME="$(echo "$LINE" |cut -d' ' -f2)"
   94         SCRIPT_CUR_TIME="$(echo "$LINE" |cut -d' ' -f3)"
   95         SCRIPT_PATH="$(echo "$LINE" |cut -d' ' -f1,2,3 --complement)"
   96 
   97         # Increment current time (in minutes)
   98         SCRIPT_CUR_TIME=$((SCRIPT_CUR_TIME + SLEEP_TIME))
   99 
  100         [ $VERBOSE -eq 1 ] && echo "Processing job \"$SCRIPT_NAME\"..."
  101 
  102         if [ $SCRIPT_CUR_TIME -ge $SCRIPT_SET_TIME ]; then
  103           [ $VERBOSE -eq 1 ] && echo " Countdown reached, running job \"$SCRIPT_NAME\"..."
  104 
  105           # Create extra FD
  106           exec 3>&1
  107 
  108           if [ $VERBOSE -ne 1 ]; then
  109             exec 3>/dev/null
  110           fi
  111 
  112           result="$("$JOB_EXECUTER" "$SCRIPT_PATH" 2>&1 1>&3)"
  113           retval=$?
  114 
  115           # Release extra FD
  116           exec 3>&-
  117 
  118           # In case an error occurred, log it
  119           if [ $retval -ne 0 ]; then
  120             echo "$result (error=$retval)" >&2
  121             echo "$result (error=$retval)" |log_msg "${SCRIPT_NAME}: "
  122           elif [ -n "$result" -a $VERBOSE -eq 1 ]; then
  123             echo "$result"
  124           fi
  125 
  126           # Reset counter
  127           SCRIPT_CUR_TIME=0
  128         fi
  129 
  130         # As soon as the jobs file gets removed, stop processing (and terminate)
  131         if [ ! -f "$JOBS_FILE" ]; then
  132           # Leave critical section:
  133           lock_leave "$JOBS_LOCK_NAME"
  134 
  135           return 0 # We're done
  136         fi
  137 
  138         # Update job
  139         sed -i "s,^$SCRIPT_NAME[[:blank:]].*,$SCRIPT_NAME $SCRIPT_SET_TIME $SCRIPT_CUR_TIME $SCRIPT_PATH," "$JOBS_FILE"
  140       done
  141     fi
  142 
  143     # Leave critical section:
  144     lock_leave "$JOBS_LOCK_NAME"
  145     #if ! lock_leave "$JOBS_LOCK_NAME"; then
  146     #  [ $VERBOSE -eq 1 ] && echo "Lock removal failed, terminating..."
  147     #  return 1
  148     #fi
  149 
  150     [ $VERBOSE -eq 1 ] && echo "Sleeping $SLEEP_TIME minutes..."
  151 
  152     # Idle wait SLEEP_TIME minutes unless the jobs gets removed
  153     if idle_wait "$SLEEP_TIME" "$JOBS_FILE"; then
  154       [ $VERBOSE -eq 1 ] && echo "Jobs file removed, terminating..."
  155       return 0 # No jobs file: We're done
  156     fi
  157   done
  158 
  159   return 0
  160 }
  161 
  162 
  163 show_help()
  164 {
  165   echo "Usage: $(basename $0) [options]" >&2
  166   echo "" >&2
  167   echo "Options:" >&2
  168   echo "--help|-h                   - Print this help" >&2
  169   echo "--verbose                   - Be verbose with displaying info (only recommended when not backgrounded!)" >&2
  170   echo "--sleep=n|-s=n              - Sleep n minutes after each run (default is 1)" >&2
  171   echo ""
  172 }
  173 
  174 
  175 process_commandline()
  176 {
  177   # Check arguments
  178   while [ -n "$1" ]; do
  179     ARG="$1"
  180     ARGNAME=`echo "$ARG" |cut -d= -f1`
  181     ARGVAL=`echo "$ARG" |cut -d= -f2 -s`
  182 
  183     case "$ARGNAME" in
  184              --sleep|-s) SLEEP_TIME="$ARGVAL";;
  185            --verbose|-v) VERBOSE=1;;
  186               --help|-h) show_help;
  187                          exit 0
  188                          ;;
  189                      -*) echo "ERROR: Bad argument \"$ARG\"" >&2
  190                          show_help
  191                          exit 1
  192                          ;;
  193                       *) echo "ERROR: Bad command syntax with argument \"$ARG\"" >&2
  194                          show_help
  195                          exit 1
  196                          ;;
  197     esac
  198 
  199     shift # Next argument
  200   done
  201 }
  202 
  203 
  204 ############
  205 # Mainline #
  206 ############
  207 
  208 process_commandline $*
  209 
  210 if [ ! -f "$CONF_FILE" ]; then
  211   echo "ERROR: Could not read configuration file ($CONF_FILE)!" >&2
  212   echo "" >&2
  213   exit 1
  214 fi
  215 
  216 # Source config file
  217 . "$CONF_FILE"
  218 
  219 # Check if the environment file exists and if so, load it
  220 #########################################################
  221 ENV_FILE="${0%/*}/environment"
  222 
  223 if [ ! -f "$ENV_FILE" ]; then
  224   echo "** ERROR: Unable to locate environment file \"$ENV_FILE\"!" >&2
  225   exit 2
  226 fi
  227 
  228 # Source environment file
  229 . "$ENV_FILE"
  230 
  231 # Only proceed if environment ok
  232 if sanity_check; then
  233   job_process
  234 fi