* PASSWD
* PASSWORD verb
* Copyright (c) 2007 Ladybridge Systems, All Rights Reserved
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
* 
* Ladybridge Systems can be contacted via the www.openqm.com web site.
* 
* START-HISTORY:
* 11 Apr 07  2.5-2 Renamed ENCRYPT() as PWCRYPT().
* 18 Oct 04  2.0-5 Use message handler.
* 16 Sep 04  2.0-1 OpenQM launch. Earlier history details suppressed.
* END-HISTORY
*
* START-DESCRIPTION:
*
* PASSWORD {username}
*
* END-DESCRIPTION
*
* START-CODE

$internal
$flags trusted
program $password
$catalog $PASSWD

$include int$keys.h
$include parser.h
$include err.h

   prompt ''

   parser = "!PARSER"
   letters= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
   others = '0123456789.-'

   @system.return.code = -ER$ARGS     ;* Preset for argument errors

   if not(system(91)) then
      stop sysmsg(6075) ;* Passwords are set using Linux tools on this platform
   end

   if system(1006) then
      stop sysmsg(6076) ;* Passwords are set using Windows administration tools on this platform
   end

   is.administrator = kernel(K$ADMINISTRATOR,-1)

   openpath @qmsys:@ds:'$LOGINS' to lgn.f else
      @system.return.code = -status()
      stop sysmsg(6050) ;* Cannot open $LOGINS register in QMSYS account
   end


   call @parser(parser$reset, 0, @sentence, 0)
   call @parser(parser$get.token, token.type, token, keyword) ;* Verb

   * Check for user name

   username = @logname
   call @parser(parser$get.token, token.type, token, keyword)
   if token.type # PARSER$END and is.administrator then
      if system(91) then username = upcase(token)

      * Validate user name

      if len(username) > MAX.USERNAME.LEN or convert(letters:others, '', username) # '' then
         stop sysmsg(6074) ;* Invalid user name
      end

      call @parser(parser$get.token, token.type, token, keyword)
   end else
****      if system(42) = '' then stop 'Not allowed for console session'
   end

   * Check for unwanted arguments

   if token.type # PARSER$END then stop sysmsg(2018, token) ;* Unexpected token (xx)


   * Check user exists in $LOGINS file.

   readu lgn.rec from lgn.f, username else
      stop sysmsg(6057) ;* User is not in register
   end

   * Prompt for old password if not administrator

   if not(is.administrator) then
      display sysmsg(6067) :  ;* Old password:
      echo off
      input password
      echo on
      display

      if pwcrypt(password) # lgn.rec<LGN$PASSWORD> then
         display sysmsg(6068) ;* Incorrect password
         sleep 3
         stop
      end
   end

   * Prompt for new password

   display sysmsg(6069) :  ;* New password:
   echo off
   input password
   echo on
   display

   display sysmsg(6070) :  ;* Re-enter password:
   echo off
   input s
   echo on
   display

   if s # password then stop sysmsg(6071) ;* Password does not match

   min.pw = lgn.rec<LGN$MIN> + 0
   if len(s) < min.pw or len(s) > 32 then
      stop sysmsg(6072, min.pw) ;* Password length must be in range %1 to 32 characters
   end


   * Modify $LOGINS entry

   lgn.rec<LGN$PASSWORD> = pwcrypt(password)
   write lgn.rec to lgn.f, username
   close lgn.f

   @system.return.code = 0

   return
end

* END-CODE
