#!/bin/sh
# vyatta_config_ssh.sh  Loads private ssh key and disables passwd auth

export PATH=$PATH:/opt/vyatta/bin:/opt/vyatta/sbin
SHELL_API=/bin/cli-shell-api
SET=/opt/vyatta/sbin/my_set
DELETE=/opt/vyatta/sbin/my_delete
COMMIT=/opt/vyatta/sbin/my_commit
SAVE=/opt/vyatta/sbin/vyatta-save-config.pl
LOADKEY=/opt/vyatta/sbin/vyatta-load-user-key.pl

#Setup config session
session_env=$($SHELL_API getSessionEnv $PPID)
  if [ $? -ne 0 ]; then
    echo "An error occured while configuring session environment!"
    exit 0
  fi
eval $session_env
$SHELL_API setupSession
  if [ $? -ne 0 ]; then
    echo "An error occured while setting up the configuration session!"
    exit 0
  fi
# Writing ssh private ket to the config
#$LOADKEY vyatta /tmp/my-key
if [ -e /tmp/my-key ]; then
  $LOADKEY vyatta /tmp/my-key
fi

# disabling ssh authentication using passwd
$SET service ssh disable-password-authentication

# deleting the password for vyatta
$DELETE system login user vyatta authentication plaintext-password
$SET system login user vyatta authentication encrypted-password '*'

$COMMIT
$SAVE

#Tear down the session
$SHELL_API teardownSession
  if [ $? -ne 0 ]; then
    echo "An error occured while tearing down the session!"
    exit 0
  fi
exit 0

