Path: blob/master/PHPMailer/test/fakepopserver.sh
1507 views
#!/usr/bin/env bash12# Fake POP3 server3# By Marcus Bointon <[email protected]>4# Copyright 2010 - 2020 Marcus Bointon5# Based on code by 'Frater' found at http://www.linuxquestions.org/questions/programming-9/fake-pop3-server-to-capture-pop3-passwords-9337336# Does not actually serve any mail, but supports commands sufficient to test POP-before SMTP7# Can be run directly from a shell like this:8# mkfifo fifo; nc -l 1100 <fifo |./fakepopserver.sh >fifo; rm fifo9# It will accept any user name and will return a positive response for the password 'test'1011# Licensed under the GNU Lesser General Public License: http://www.gnu.org/copyleft/lesser.html1213# Enable debug output14#set -xv15export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin1617LOGFOLDER=/tmp1819LOGFILE=${LOGFOLDER}/fakepop.log2021LOGGING=122DEBUG=123TIMEOUT=602425POP_USER=26POP_PASSWRD=test2728LINES=129BREAK=03031write_log () {32if [ ${LINES} -eq 1 ] ; then33echo '---' >>${LOGFILE}34fi35let LINES+=136[ ${LOGGING} = 0 ] || echo -e "`date '+%b %d %H:%M'` pop3 $*" >>${LOGFILE}37}3839ANSWER="+OK Fake POP3 Service Ready"4041while [ ${BREAK} -eq 0 ] ; do42echo -en "${ANSWER}\r\n"4344REPLY=""4546#Input appears in $REPLY47read -t ${TIMEOUT}4849ANSWER="+OK "50COMMAND=""51ARGS=""52TIMEOUT=305354if [ "$REPLY" ] ; then55write_log "RAW input: '`echo "${REPLY}" | tr -cd '[ -~]'`'"5657COMMAND="`echo "${REPLY}" | awk '{print $1}' | tr -cd '\40-\176' | tr 'a-z' 'A-Z'`"58ARGS="`echo "${REPLY}" | tr -cd '\40-\176' | awk '{for(i=2;i<=NF;i++){printf "%s ", $i};printf "\n"}' | sed 's/ $//'`"5960write_log "Command: \"${COMMAND}\""61write_log "Arguments: \"${ARGS}\""6263case "$COMMAND" in64QUIT)65break66;;67USER)68if [ -n "${ARGS}" ] ; then69POP_USER="${ARGS}"70ANSWER="+OK Please send PASS command"71fi72;;73AUTH)74ANSWER="+OK \r\n."75;;76CAPA)77ANSWER="+OK Capabilities include\r\nUSER\r\nCAPA\r\n."78;;79PASS)80if [ "${POP_PASSWRD}" == "${ARGS}" ] ; then81ANSWER="+OK Logged in."82AUTH=183else84ANSWER="-ERR Login failed."85fi86;;87LIST)88if [ "${AUTH}" = 0 ] ; then89ANSWER="-ERR Not authenticated"90else91if [ -z "${ARGS}" ] ; then92ANSWER="+OK No messages, really\r\n."93else94ANSWER="-ERR No messages, no list, no status"95fi96fi97;;98RSET)99ANSWER="+OK Resetting or whatever\r\n."100;;101LAST)102if [ "${AUTH}" = 0 ] ; then103ANSWER="-ERR Not authenticated"104else105ANSWER="+OK 0"106fi107;;108STAT)109if [ "${AUTH}" = 0 ] ; then110ANSWER="-ERR Not authenticated"111else112ANSWER="+OK 0 0"113fi114;;115NOOP)116ANSWER="+OK Hang on, doing nothing"117;;118esac119else120echo "+OK Connection timed out"121break122fi123done124125echo "+OK Bye!\r\n"126127128