#!/usr/bin/bash
#
# Copyright (c) 2003 Marcus Williams <marcus@quintic.co.uk>
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
#
# Implements a simple nonce algorithm
#
# Usage: ./nonce.sh --create [n]   Creates a new key id for use n times. No
#                                  number means once
#                   --check <key>  Checks a key id if it can be used.
#                   --purge [key]  Removes a key or all keys if no key
#                                  specified
#
# Exit conditions are set up for use with bouncesaying (0 means bounce
# 1 means no bounce).
#
# To use with qmail add two .qmail files:
#
# .qmail-user:
#
# | /var/qmail/bin/bouncesaying "No such user at this address"
#
# .qmail-user-default
#
# | /var/qmail/bin/bouncesaying "Reason for bounce" /path/to/nonce.sh  --check "$EXT"
# ./Maildir/
#
# Create a key and then mail user-keyid@yourdomain to test.
#
# NOTE: Be careful using this in email as you are letting an untrusted
# source feed arguments to the shell script ($EXT).
#
# The latest version of nonce.sh can be found at http://www.thesafebox.com

KEYDIR=~/.keys
EXIT=1

if [ ! -e $KEYDIR ] 
then 
  mkdir $KEYDIR
  chmod 0700 $KEYDIR
fi

case $1 in
  "--create" ) 
    N=${2:-1}
    TIME=`date +%s`

    while [ -e $KEYDIR/$TIME.$$.* ]
    do
      sleep 1
      TIME=`date +%s`
    done
     
    touch $KEYDIR/$TIME.$$.$N
    echo $TIME.$$
    ;;
  "--check" ) 
    # make sure key is all numbers and one digit

    if expr x"$2" : 'x[0-9][0-9]*\.[0-9][0-9]*$' > /dev/null
    then
      if [ ! -e $KEYDIR/$2.* ] 
      then EXIT=0
      else 
        # file exists but has a count
        COUNT=`(cd $KEYDIR; ls $2.*) | awk -F. '{printf $3}'`
        rm $KEYDIR/$2.*
        COUNT=$(($COUNT-1))
        if [ $COUNT -lt 0 ]
        then EXIT=0
        else touch $KEYDIR/$2.$COUNT
        fi
      fi
    else
      EXIT=0
    fi
    ;;
  "--purge" )
    if [ $2 ]
    then
      if expr x"$2" : 'x[0-9][0-9]*\.[0-9][0-9]*$' > /dev/null
      then
        rm $KEYDIR/$2.*
      fi
    else
      # remove all keys
      rm $KEYDIR/*
    fi
    ;;
esac

exit $EXIT
