#!/usr/bin/python
#
# @package      hubzero-filexfer-xlate
# @file         filexfer-xlate
# @copyright    Copyright (c) 2005-2020 The Regents of the University of California.
# @license      http://opensource.org/licenses/MIT MIT
#
# Copyright (c) 2005-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.

""" URL translation service for Apache.
Deployment: master host (web server)
This file is called based on settings in the following files in /etc/apache2:

sites-available/hub-ssl:	RewriteMap	xlate		prg:/usr/bin/filexfer-xlate
sites-available/hub-ssl:		RewriteRule	^filexfer/(.*)		${xlate:$1|nothing} [P,QSA,L]
sites-enabled/hub-ssl:	RewriteMap	xlate		prg:/usr/bin/filexfer-xlate
sites-enabled/hub-ssl:		RewriteRule	^filexfer/(.*)		${xlate:$1|nothing} [P,QSA,L]

Last modified by Pascal Meunier June 7 2010:
removed SQL injection, used mw_common, tidied up, entered comments

"""

import sys
import os
import MySQLdb
import re
import time

ALPHANUM_REGEXP = r'\A[a-zA-Z0-9]+\Z'
VERBOSE = False
logfile_name = "/var/log/filexfer-xlate/filexfer-xlate.log"

if os.path.exists("/etc/mw-client/mw2-client.conf"):
  config_file="/etc/mw-client/mw2-client.conf"
elif os.path.exists("/etc/mw-client/mw-client.conf"):
  config_file="/etc/mw-client/mw-client.conf"
elif os.path.exists("/etc/mw-www/maxwell.conf"):
  config_file="/etc/mw-www/maxwell.conf"
elif os.path.exists("/etc/hubzero/maxwell.conf"):
  config_file="/etc/hubzero/maxwell.conf"
elif os.path.exists("/etc/mw/maxwell.conf"):
  config_file="/etc/mw/maxwell.conf"
else:
  config_file=""

# configuration variables
mysql_host = None
mysql_user = None
mysql_password = None
mysql_db = None

#=============================================================================
# Load the configuration and override the variables above.
#=============================================================================
try:
  execfile(config_file)
except IOError:
  print "The configuration file '%s' is not readable" % (config_file)
  sys.exit(1)

#=============================================================================
# Set up errors to go to the log file.
#=============================================================================
def openlog(filename):
  if not os.isatty(2):
    try:
      log = open(filename,"a+")
      os.dup2(log.fileno(), sys.stderr.fileno())
      log.close()
    except:
      pass

#=============================================================================
# Log a message.
#=============================================================================
def log(msg):
  timestamp = time.strftime("%m/%d %H:%M:%S")
  msg = "[%s]%d: %s\n" % (timestamp, os.getpid(), msg)
  if (os.isatty(2)):
    print msg
  else:
    sys.stderr.write(msg + "\n")

#=============================================================================
# Create database connection.
#=============================================================================
def db_connect():
  try:
    db = MySQLdb.connect(host=mysql_host, user=mysql_user, passwd=mysql_password, db=mysql_db)
    return db
  except:
    log("Exception in db_connect")

#=============================================================================
# MySQL helpers
#=============================================================================
def mysql(c,cmd):
  try:
    count = c.execute(cmd)
    return c.fetchall()
  except MySQLdb.MySQLError, (num, expl):
    log("%s.  SQL was: %s" % (expl,cmd))
    return ()
  except:
    log("Some other MySQL exception.")
    return ()

def mysql_act(c,cmd):
  try:
    count = c.execute(cmd)
    return ""
  except MySQLdb.MySQLError, (num, expl):
    return expl

#=============================================================================
# Main program
#=============================================================================

openlog(logfile_name)
log("Starting up.")

while 1:
  try:
    line = sys.stdin.readline()
    if line == "":
      log("No input")
      break

    line = line.strip()
    i = line.find("/")
    cookie = line[0:i]
    uri = line[i:]
    m = re.match(ALPHANUM_REGEXP, cookie)
    if m is None:
      raise StandardError("Bad cookie '%s'" % cookie)

    db = db_connect()
    c = db.cursor()

    arr = mysql(c,"""SELECT fileperm.sessnum,fileuser,hostname,dispnum
                     FROM fileperm join display
                     ON fileperm.sessnum = display.sessnum
                     WHERE fileperm.cookie='%s'""" % cookie)
    db.close()

    trans = "NULL"

    if len(arr) != 1:
      log("No database entry found for ID %s." % cookie)
    else:
      sess = arr[0][0]
      user = arr[0][1]
      host = arr[0][2]
      disp = arr[0][3]
      port = disp + 9000
      trans = "http://" + host + ":" + str(port) + uri
      log("Translation is %s" % trans)

    sys.stdout.write(trans + "\n")
    sys.stdout.flush()
    if VERBOSE:
       log("Done")

  except:
    log("Exception: " + str(sys.exc_info()[0]))
    sys.stdout.write("\n")
    sys.stdout.flush()

log("Exiting while loop???")

# If the while loop finishes for some reason...
sys.exit(0)
