#!/bin/sh
# Command-line interaction script for the warden
# Author: Kris Moore
# License: BSD
# "The Warden" Copyright 2008 PC-BSD Software (iXsystems)
######################################################################

UTEST="`whoami`"
if [ "$UTEST" != "root" ]
then
   echo "ERROR: The Warden must be run as root!"
   exit 155
fi


# Source our functions
PROGDIR="`pbreg get /PC-BSD/TheWarden/ProgDir`"

if [ -z "${PROGDIR}" ]
then
   echo "PROGDIR unset! Is The Warden installed properly?"
   exit 155
fi

# Source our variables
. ${PROGDIR}/conf/warden.conf

title()
{
  echo "The Warden version ${WARDENVER}
---------------------------------"
};


# Function to display help information
help_main()
{
  title
  echo "Available commands

Type in help <command> for infomation and usage about that command

  help - This help file
  menu - Launch the text-based menu
   gui - Launch the GUI menu

    auto - Toggles the autostart flag for a jail
  chroot - Launches chroot into a jail 
  create - Creates a new jail 
  delete - Deletes a jail
  export - Exports a jail to a .wdn file
  inload - Install an inmate package into a jail
   indel - Deletes an inmate from a jail
  import - Imports a jail from a .wdn file
    list - Lists the installed jails
mkinmate - Creates a inmate file from the specified directory
    pkgs - Lists the installed packages / inmates in a jail
   start - Start a jail
    stop - Stops a jail
"
};

help_mkinmate()
{
 title
 echo "Help mkinmate

Creates a warden inmate file (.wit) from a specified directory and 
its contents. The finished .wit file will be created in your PWD. 

NOTE: Please see the warden's documentation for information on the 
inmate format and a creation guide. 

Usage:

  warden mkinmate <Inmate Directory>

Example:

  warden mkinmate /root/AMP-inmate
"
};

help_inload()
{
 title
 echo "Help inload

Loads the specified inmate package into a specified jail. The command
returns 0 on success. 

Usage:

  warden inload <IP> <Inmate File>

Example:

  warden inload 192.168.0.5 /root/AMP.wit
"
};

help_indel()
{
 title
 echo "Help indel

Loads the specified inmate package into a specified jail. The command
returns 0 on success.

Usage:

  warden indel <IP> <Inmate Package>

Example:

  warden indel 192.168.0.5 AMP1.0
"
};

help_auto()
{
 title
 echo "Help auto

Toggles the autostart flag for a specified jail. If the flag is 
enabled, then the jail will be started at system bootup automatically.
The flag status can be checked with 'warden list'. 

Usage:

  warden auto <IP>

Example:

  warden auto 192.168.0.5
"
};

help_pkgs()
{
 title
 echo "Help pkgs

Generates a listing of the installed ports in a jail.

Usage:

  warden pkgs

Example:

  warden pkgs 192.168.0.5
"
};

help_list()
{
 title
 echo "Help list

Generates a quick listing of the installed jails, including the running
status.

Usage:

  warden list
"
};

help_export()
{
 title
 echo "Help export

Export will backup a full copy of your jail, compressing it and 
saving it to a .wdn file. This file can then be taken and 
imported on another system to migrate the jail to new hardware. 
Also a .wdn file may be imported on the same system, in order to 
'clone' an existing jail with a new IP address. 

Available Flags:
  --dir=<DIR>  (Specify the directory you want to place the finished .wdn file)

Usage:

  warden export <IP> --dir=<path>

Example:

  warden export 192.168.0.5 --dir=/tmp
"
};

help_import()
{
 title
 echo "Help import

Imports a jail from a .wdn file. Can assign a new IP / Hostname to
the imported jail on the fly using optional flags.  

Available Flags:
  --ip=<IP>      (Specify a new IP address to assign to this jail)
  --host=<HOST>  (Specify a new host name to assign to this jail)

Usage:

  warden import <.wdn file>

Example:

  warden import /home/kris/192.168.0.5.wdn
"
};



help_chroot()
{
 title
 echo "Help chroot

Logs into a jail using chroot

Usage:

  warden chroot <IP>

Example:

  warden chroot 192.168.0.5
"
};


help_start()
{
 title
 echo "Help start 

Starts a stopped jail

Usage:

  warden start <IP>

Example:

  warden start 192.168.0.5
"
};

help_stop()
{
 title
 echo "Help stop

Stops a running jail

Usage:
  warden stop <IP>

Example:

  warden stop 192.168.0.5
"
};


help_delete()
{
 title
 echo "Help delete

Stops and deletes a jail

Available Flags:
  --confirm       (Does not prompt to confirm deletion. DANGEROUS)

Usage:

  warden delete <IP>

Example:

  warden delete 192.168.0.5 
"
};



help_create()
{
 title
 echo "Help create

Creates a new jail, with options for system source, ports and autostarting.

Available Flags:
  --src       (Includes /usr/src system source)
  --ports     (Includes the ports tree)
  --startauto (Start this jail at system boot)

Usage:

  warden create <IP> <HOSTNAME> <flags>

Example:

  warden create 192.168.0.5 jailbird --src --ports --startauto
"
};


# Check what the user wants to do
case "$1" in

   help) case "$2" in
             auto) help_auto ;;
           create) help_create ;;
           start) help_start ;;
           stop) help_stop ;;
           delete) help_delete ;;
           chroot) help_chroot ;;
           list) help_list ;;
           export) help_export ;;
           import) help_import ;;
           pkgs) help_pkgs ;;
           inload) help_inload ;;
           indel) help_indel ;;
           mkinmate) help_mkinmate ;;
           *) help_main ;;
         esac  ;;


    pkgs) IP="${2}"

         if [ -z "${IP}" ]
         then
          echo "ERROR: No IP specified!"
          exit 1
         fi

         if [ ! -e "${JDIR}/${IP}" ]
         then
           echo "ERROR: No such jail!"
           exit 1
         fi

         ${PROGDIR}/scripts/backend/listpkgs.sh "${IP}" 

         ;;

   auto) IP="${2}"

         if [ -z "${IP}" ]
         then
          echo "ERROR: No IP specified!"
          exit 1
         fi

         if [ ! -e "${JDIR}/${IP}" ]
         then
           echo "ERROR: No such jail!"
           exit 1
         fi


         if [ -e "${JDIR}/${IP}/etc/.wardenautostart" ]
         then
            rm "${JDIR}/${IP}/etc/.wardenautostart"
            echo "Disabled autostart for ${IP}"
         else
            touch "${JDIR}/${IP}/etc/.wardenautostart"
            echo "Enabled autostart for ${IP}"
         fi 

         ;;

   menu) ${PROGDIR}/scripts/warden.sh ;;

    gui) ${PROGDIR}/bin/warden-gui ;;

    list) ${PROGDIR}/scripts/backend/listjails.sh ;;
    
   start) # Time to startup the jail
          IP="${2}"

          if [ -z "${IP}" ]
          then
           echo "ERROR: No IP specified!"
           exit 1
          fi

          if [ ! -e "${JDIR}/${IP}" ]
          then
            echo "ERROR: No such jail!"
            exit 1
          fi 
         
          # Check if this jail is already running
          ${PROGDIR}/scripts/backend/checkstatus.sh "${IP}"
          if [ "$?" = "0" ]
          then
            echo "ERROR: This jail is already running!"
            exit 1
          fi
 
          ${PROGDIR}/scripts/backend/startjail.sh "${IP}" 
          ;;
   stop) # Time to stop the jail
          IP="${2}"

          if [ -z "${IP}" ]
          then
           echo "ERROR: No IP specified!"
           exit 1
          fi

          if [ ! -e "${JDIR}/${IP}" ]
          then
            echo "ERROR: No such jail!"
            exit 1
          fi 
         
          # Check if this jail is already running
          ${PROGDIR}/scripts/backend/checkstatus.sh "${IP}"
          if [ "$?" != "0" ]
          then
            echo "ERROR: This jail is already stopped!"
            exit 1
          fi
 
          ${PROGDIR}/scripts/backend/stopjail.sh "${IP}" 
          ;;

 chroot) # Log a user into chroot session
          IP="${2}"

          if [ -z "${IP}" ]
          then
           echo "ERROR: No IP specified!"
           exit 1
          fi

          if [ ! -e "${JDIR}/${IP}" ]
          then
            echo "ERROR: No such jail!"
            exit 1
          fi

          ${PROGDIR}/scripts/backend/chrootjail.sh "${IP}"
          ;;
 mkinmate) # The user wants to create an inmate package, do it
         IDIR="${2}"

         if [ -z "${IDIR}" ]
         then
           echo "ERROR: No inmate directory specified!"
           exit 1
         fi

         # Pass the values off to the backend now
         ${PROGDIR}/scripts/backend/mkinmate.sh "${IDIR}" ""
        ;;

 inload) # The user wants to install an inmate package, do it 
         IP="${2}"
         IFILE="${3}"

         if [ ! -e "$IFILE" ]
         then
           echo "ERROR: Invalid filename: ${IFILE}"
           exit 1
         fi

         if [ -z "${IP}" ]
         then
           echo "ERROR: No IP specified!"
           exit 1
         fi

         if [ ! -e "${JDIR}/${IP}" ]
         then
            echo "ERROR: No such jail!"
            exit 1
         fi

         ${PROGDIR}/scripts/backend/installinmate.sh "${IP}" "${IFILE}"
         ;;
 indel) # The user wants to remove an inmate package, do it
         IP="${2}"
         IPKG="${3}"

         if [ -z "${IP}" ]
         then
           echo "ERROR: No IP specified!"
           exit 1
         fi

         if [ ! -e "${JDIR}/${IP}" ]
         then
            echo "ERROR: No such jail!"
            exit 1
         fi

         ${PROGDIR}/scripts/backend/deleteinmate.sh "${IP}" "${IPKG}"
         ;;

 import) # The user wants to import a jail, lets do it!
         IFILE="$2"
         if [ ! -e "$IFILE" ]
         then
           echo "ERROR: Invalid filename: ${IFILE}"
           exit 1
         fi

         IP="OFF"
         HOST="OFF"
         for i in "$@"
         do
           # Check if we have a new IP for this import
           echo "${i}" | grep '\-\-ip=' >/dev/null 2>/dev/null
           if [ "$?" = "0" ]
           then
              IP="`echo ${i} | cut -d '=' -f 2`"

              #Sanity check on the IP
              check_ip ${IP}
              if [ "$?" != "0" ]
              then
                echo "ERROR: Invalid IP address!"
                exit 1
              fi

              # Check if the directory already exists
              if [ -e "${JDIR}/${IP}" ]
              then
                 echo "ERROR: A jail with this IP already exists!"
                 exit 1
              fi

           fi
  
           # Check if we have a new hostname for this jail
           echo ${i} | grep '\-\-host=' >/dev/null 2>/dev/null
           if [ "$?" = "0" ]
           then
              HOST="`echo ${i} | cut -d '=' -f 2`"
           fi

         done

          # Import the jail now!
          ${PROGDIR}/scripts/backend/importjail.sh "${IFILE}" "${IP}" "${HOST}"
        ;;

 export) # The user wants to export a jail, lets do it!
         IP="$2"
         if [ -z "$IP" ]
         then
           echo "ERROR: No IP specified!"
           exit 1
         fi

         if [ ! -e "${JDIR}/${IP}" ]
         then
           echo "ERROR: No such jail!"
           exit 1
         fi

         # Check if the user wants to place the file in their own DIR
         DIR=""
         for i in "$@"
         do
           echo ${i} | grep "\-\-dir=" >/dev/null 2>/dev/null
           if [ "$?" = "0" ]
           then
              DIR="`echo ${i} | cut -d '=' -f 2`"
              # Check if the directory exists
              if [ ! -d "${DIR}" ]
              then
                 echo "ERROR: No such directory ${DIR}!"
                 exit 1
              fi
           fi

         done

         # Export the jail now
         ${PROGDIR}/scripts/backend/exportjail.sh "${IP}" "${DIR}"

         ;;

 create) # The user wants to create a new jail, do error checking
         IP="$2"
         HOST="$3"
         if [ -z "$IP" -o -z "$HOST" ]
         then
           echo "ERROR: No IP / Host specified!"
           exit 1
         fi

         #Sanity check on the IP
         check_ip ${IP}
         if [ "$?" != "0" ]
         then
           echo "ERROR: Invalid IP address!"
           exit 1
         fi

         #Now check for the presence of the optional flags
         SRC="NO"
         PORTS="NO"
         AUTO="NO"
         for i in "$@"
         do
           case $i in
             --src) SRC="YES" ;;
             --ports) PORTS="YES" ;;
             --startauto) AUTO="YES" ;;
           esac
         done

         # Check to ensure this jail does not already exist
         if [ -e "${JDIR}/${IP}" ]
         then
           echo "ERROR: A jail with this IP already exists!"
           exit 1
         fi  

          # Passed all tests, create the jail now
          ${PROGDIR}/scripts/backend/createjail.sh "${IP}" "${HOST}" "${SRC}" "${PORTS}" "${AUTO}"

        ;;
  delete) # Time to delete a jail
          IP="${2}"

          if [ -z "${IP}" ]
          then
           echo "ERROR: No IP specified!"
           exit 1
          fi

          if [ ! -e "${JDIR}/${IP}" ]
          then
            echo "ERROR: No such jail!"
            exit 1
          fi

          if [ "$3" != "--confirm" ]
          then
            echo "Are you sure you want to delete ${IP}?"
            echo -e "Choice: (y/n)\c"
            read confirm
            if [ "$confirm" != "y" ]
            then
              echo "Canceled!"
              exit 1 
            fi
          fi

          # Check if this jail is running
          ${PROGDIR}/scripts/backend/checkstatus.sh "${IP}"
          if [ "$?" = "0" ]
          then
            ${PROGDIR}/scripts/backend/stopjail.sh "${IP}"
          fi

          # Delete it now
          ${PROGDIR}/scripts/backend/deletejail.sh "${IP}"
        ;;
   *) help_main ;;
esac

exit 0


