#****************************************************************************
#  ##   ##         #####   #####  ##     **      NoSQL RDBMS - pwmatch8     *
#  ###  ##        ####### ####### ##     **        $Revision: 2.1 $			*
#  #### ##        ###     ##   ## ##     ************************************
#  #######  ####  #####   ##   ## ##     **      Carlo Strozzi (c) 1998     *
#  ####### ######   ##### ## # ## ##     ************************************
#  ## #### ##  ##     ### ##  ### ##     **           Written by            *
#  ##  ### ###### ####### ######  ###### **          Carlo Strozzi          *
#  ##   ##  ####   #####   #### # ###### **     e-mail: carlos@linux.it     *
#****************************************************************************
#   NoSQL RDBMS, Copyright (C) 1998 Carlo Strozzi.                          *
#   This program comes with ABSOLUTELY NO WARRANTY; for details             *
#   refer to the GNU General Public License.                                *
#****************************************************************************
#
# Takes two environment variables, PW_CLEAR and PW_CRYPT, and checks whether
# the former is the plaintext version of the latter. Returns 0 if it is,
# or 255 if it is not. 
#
# Handles cleartext passwords of max. 8 characters, while PW_CRYPT must
# be in the usual 13-byte format. If either variables are longer they will
# be truncated to fit.
#
#****************************************************************************

BEGIN \
{
  NULL = ""

  # Check whether 'verbose' was specified.
  if ( __nosql_args == "-v" || __nosql_args == "--verbose" ) verbose = 1

  if ( ENVIRON["PW_CLEAR"] == NULL || ENVIRON["PW_CRYPT"] == NULL )
  { 
	if ( verbose )
	  print \
		"nosql pwmatch8: either PW_CLEAR or PW_CRYPT not set" > "/dev/stderr"

	exit 1
  }

  pwclear = substr( ENVIRON["PW_CLEAR"], 1, 8 )
  pwcrypt = substr( ENVIRON["PW_CRYPT"], 1, 13 )
  old_salt = substr( pwcrypt, 1, 2 )
  unix_cmd = "echo -e '" pwclear "\n" old_salt "' | ${NSQLIB}/bin/crypt8"
  unix_cmd | getline cmd_output
  close( unix_cmd )

  if ( cmd_output == pwcrypt )
  {
	if ( verbose ) print "nosql pwmatch8: match ok" > "/dev/stderr"
	exit 0
  }
  else
  {
	if ( verbose )
	  print "nosql pwmatch8: passwords do not match" > "/dev/stderr"

	exit 255
  }
}

