#!/usr/bin/env python2
# @package      hubzero-mw2-dispatch
# @file         vssh_dispatch
# @copyright    Copyright (c) 2016-2020 The Regents of the University of California.
# @license      http://opensource.org/licenses/MIT MIT
#
# Copyright (c) 2016-2020 The Regents of the University of California.
#
# 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.
#
# HUBzero is a registered trademark of The Regents of the University of California.
#

"""
This script is like the "dispatch" script but only allows commands used for Virtual SSH.
It is intended to be used as an SSH forced command that validates incoming commands
and calls the appropriate scripts.
Deployment: execution hosts (calling /usr/bin/maxwell_service and virtual SSH commands)
"""

import sys
import os
import glob
from hubzero.mw.constants import VSSH_SERVICE_LOG, HOST_K, VIRTUALSSH_K, EXEC_CONFIG_FILE, FILE_CONFIG_FILE

HOST_CONF = {}
try:
  if os.path.isfile(EXEC_CONFIG_FILE):
    execfile(EXEC_CONFIG_FILE)
except EnvironmentError:
  print "Unable to read configuration file, exiting."
  print "The configuration file '%s' or '%s' needs to exist" % (EXEC_CONFIG_FILE, FILE_CONFIG_FILE)
  sys.exit(1)

HOST_MERGED = HOST_K
HOST_MERGED.update(HOST_CONF)

# old name virtualssh_client, new name vssh_exec_proxy
CLIENT_PATHS = ['/usr/bin/virtualssh_client', '/usr/bin/vssh_exec_proxy']

def log(msg):
  l = open(VSSH_SERVICE_LOG, "a")
  l.write("vssh_dispatch: " + sys.argv[0] + ": " + msg + "\n")
  l.close()

def err(msg):
  open(SERVICE_LOG,"a").write("vssh_dispatch: ERROR: " + sys.argv[0] + ": " + msg + "\n")
  #sys.stderr.write(sys.argv[0] + ": " + msg + "\n")
  #syslog.syslog(syslog.LOG_ERR, sys.argv[0] + ": " + msg)
  sys.exit(1)

if not os.environ.has_key("SSH_ORIGINAL_COMMAND"):
  err("invoked without SSH command")

command = os.environ["SSH_ORIGINAL_COMMAND"]
args = command.split()
log("invoking: %s" % command)

#
# Virtual SSH by users, using credentials in /tmp/ssh-passthru-XXXXXX
if args[0] in CLIENT_PATHS:
  os.execve(args[0], args, os.environ)
  err("failed execve()")

#
# Error...
#
err("Unrecognized command: " + str.join(' ', args))

