#!/usr/bin/bash
 
if [ $(/usr/bin/id -u) != 0 ]; then echo "only root can do that"; exit 2; fi

#***************************************************************************
# This file is part of the CRYPTO BONE
# File     : checkemail
# Version  : 1.5 (External Cryptobone)
# License  : BSD
# Date     : Wednesday, 1 March 2023
# Contact  : Please send enquiries and bug-reports to innovation@senderek.ie
#
#
# Copyright (c) 2015-2023
#	Ralf Senderek, Ireland.  All rights reserved. (https://senderek.ie)
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. All advertising materials mentioning features or use of this software
#    must display the following acknowledgement:
#	   This product includes software developed by Ralf Senderek.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND  ANY EXPRESS OR 
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE  IMPLIED WARRANTIES 
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
# POSSIBILITY OF SUCH DAMAGE.
#
#****************************************************************************

RAM=/dev/shm/EXRAM
IN=$RAM/f.IN
OUT=$RAM/f.OUT

if [ $# -gt 0 ]
then
     if [ $1 = "IN" ]
     then
          if [ -r $OUT ]
          then
	       # an attempt of sending a test message happend before
               /bin/rm -f $RAM/incoming 2> /dev/null
	       # wait for fetchmail to collect the test message
	       sleep 50
               /bin/ls $RAM/fetch-*  2> /dev/null | sort > $IN
	       sleep 1
	       # check if a new fetchmail file has arrived
	       /bin/diff $OUT $IN | cut -c3- | tail -1 > $RAM/incoming
               sleep 1
               if [ -s $RAM/incoming ]
               then
                    # new email has come in 
                    if grep "unencrypted test email from root" $(cat $RAM/incoming) 2> /dev/null
                    then
                         echo "TESTMESSAGE received successfully." 
                    fi        
               else
                    # there is a problem receiving mail 
                    sleep 5
                    echo "TESTMESSAGE cannot be received."
                    /bin/cat $RAM/fetchmail.output 2> /dev/null | tail -6
                    exit 2
               fi

	  else
               echo "No testmessage was sent out."
	       echo "Exiting ..."
	       exit 3
	  fi
     fi

     if [ $1 = "OUT" ]
     then
          # remove output and error messages
          /bin/rm -f $RAM/msmtpout $RAM/msmtperr 2> /dev/null
          /bin/rm -f $RAM/fetchmail.output $RAM/fetchmail.error 2> /dev/null

          if [ $# -gt 1 ]
	  then
	       # clear any remaining signals
	       /bin/rm -f $RAM/outgoing $RAM/outgoing2 2> /dev/null
	       /bin/rm -f $OUT $IN 2> /dev/null
	       # register root's email before email is sent out
	       /bin/ls -l /var/spool/mail/root > $RAM/outgoing 2> /dev/null
	       SENDER="root"
               RECIPIENT="$2"
	       SMTPSERVER="$3"
	       # trying to send an email out
	       if [ ${SMTPSERVER}x != "nonex" ] 
	       then
	            echo "using msmtp"
                    echo -e 'Subject: test\n\nunencrypted test email from root\n' |  /usr/bin/msmtp -C/usr/lib/cryptobone/ext/.exmsmtprc -d -- "$RECIPIENT" > $RAM/msmtpout 2> $RAM/msmtperr
		    DONE=$?
	       else
		    if ls -l /etc/alternatives/mta | grep sendmail > /dev/null 2> /dev/null
		    then 
	                 echo "using sendmail"
                         echo -e 'Subject: test\n\nunencrypted test email from root\n' |  /usr/sbin/sendmail -f root "$RECIPIENT"
			 DONE=$?
		    else
		         echo "Please check your local sendmail transport."
			 echo "You may have to activate and configure postfix"
			 echo "sendmail cannot be used!"
                         /bin/rm -f $RAM/outgoing  2> /dev/null
		         exit 3
	            fi		 
	       fi
	       if [ $DONE -eq 0 ] 
	       then
	            # signal that an attempt of sending an email out was successful
                    /bin/ls $RAM/fetch-* 2>/dev/null | sort > $OUT 
               fi

	       # check if root has got some email response, maybe about failed delivery
	       sleep 8
	       /bin/ls -l /var/spool/mail/root > $RAM/outgoing2 2> /dev/null
	       
	       diff $RAM/outgoing2 $RAM/outgoing > /dev/null 2> /dev/null
	       if [ $? -eq 0 ] 
	       then
	            # no new mail for root
	            if [ ${SMTPSERVER}x = "nonex" ] 
		    then
		         # sendmail was successful
                         echo "TESTMAIL sent out to $RECIPIENT"
                         /bin/rm -f $RAM/outgoing $RAM/outgoing2 2> /dev/null
                         exit 0
                    else
		         # check for errors with msmtp
                         if [ -s $RAM/msmtperr ]
			 then
			      # error is not zero
			      echo "The test email cannot be sent out due to errors:"
			      cat $RAM/msmtperr
                              /bin/rm -f $RAM/outgoing $RAM/outgoing2 $OUT  2> /dev/null
			      exit 2
                         else
                              echo "TESTMAIL sent out to $RECIPIENT"
                              /bin/rm -f $RAM/outgoing $RAM/outgoing2  2> /dev/null
                              exit 0
			 fi
		    fi
	       else 
	            echo "FAILURE sending email OUT"
		    echo "Please look for more information in root's email"
                    /bin/rm -f $RAM/outgoing $RAM/outgoing2 $OUT  2> /dev/null
		    exit 2
	       fi
	  else
	       echo "The email address is missing"
	  fi
     fi
else
     echo "missing parameter"
     exit 1
fi
exit 0
