#!/usr/bin/ensim-python

#
# Copyright (c) Ensim Corporation 2000, 2001   All Rights Reserved.
#
# This software is furnished under a license and may be used and copied
# only  in  accordance  with  the  terms  of such  license and with the
# inclusion of the above copyright notice. This software or any other
# copies thereof may not be provided or otherwise made available to any
# other person. No title to and ownership of the software is hereby
# transferred.
#
# The information in this software is subject to change without notice
# and  should  not be  construed  as  a commitment by Ensim Corporation.
# Ensim assumes no responsibility for the use or  reliability  of its
# software on equipment which is not supplied by Ensim.
#
# Exit codes (on failure error message goes to stderr):
#  0 - success
#  1 - failure
#
# This program performs a virtual domain operation. Based on the
# actual operation it does a chroot and execs the command or passes
# the virtual domain as an environment variable. Its arguments are the domain
# name, the command (with full path) and its arguments.
#
# VirtDomainOperation vx.com /usr/path/to/list_aliases
#

from vh3 import virthost
import sys
import os
import re

#these scripts are called only if their respective options (flags)
#from the virtualhosting conf file are on
restrictedscripts = {"/usr/lib/opcenter/ftp/EnableAnonFtp" : "AnonFtp",
                     "/usr/lib/opcenter/ftp/DisableAnonFtp" : "AnonFtp"}

#these scripts will be called in the chrooted environment
nonchrootscripts = ["/usr/lib/opcenter/proftpd/EnableAnonFtp",
                    "/usr/lib/opcenter/proftpd/DisableAnonFtp",
                    "/usr/lib/opcenter/proftpd/ChangeFtpOption",
                    "/usr/lib/opcenter/apache/get_config",
                    "/usr/lib/opcenter/apache/set_userpasswd",
                    "/usr/lib/opcenter/apache/remove_userpasswd",
                    "/usr/lib/opcenter/apache/get_protection",
                    "/usr/lib/opcenter/apache/protect_directory",
                    "/usr/lib/opcenter/apache/set_config",
                    "/usr/lib/opcenter/mivamerchant/get_webname",
                    "/usr/lib/opcenter/virtualhosting/ChangeDomainPasswd",
                    ]

def VirtDomainOperation(argv):
    if len(argv) < 1:
        return "The number of arguments is too small.\n"

    if argv[0] == 'localhost':
        Domain = argv.pop(0)
    else:
        Domain = virthost.get_site_from_anything(string.lower(argv[0]))
        if not Domain:
            return "Invalid Domain name %s .\n" % argv[0]
        else:
            argv.pop(0)
    
    if argv[0] == '-u':
        argv.pop(0)
        User = argv.pop(0)
    else:
        User = None

    if len(argv) < 1:
        return "The number of arguments is too small.\n"

    Program = argv[0]

    if User and not re.match(r"^[a-zA-Z0-9_-]+$", User):
        return "Invalid user name %s \n" % User

    chroot = ""
    if Domain != "localhost":
        configs = virthost.get_configs_from_site(Domain)
        if not configs:
            # TODO give proper error
            return "Unable to retrieve domain configuration"
        if restrictedscripts.has_key(Program):
            if configs[restrictedscripts[Program]]['enabled'] == '0':
                return "You are not allowed to perform this operation.\n"

        if not os.environ.has_key('OCW_SVCPATH'):
            os.environ['OCW_SVCPATH'] = "/usr/lib/opcenter"

        if not Program in nonchrootscripts:
            chroot = "/usr/sbin/chroot " + virthost.domainfs_path(Domain) + " "
        else:
            # TODO Fill environment with domain configuration
            # Will do this later...
            pass

    if User:
        # TODO figure out what the perl code is doing
        # XXX No idea... Cristi's work...
        pass
    try:
        os.execv(chroot + Program, argv)
    except OSError, (errno, strerror):
        # Print out errno
        import traceback ; traceback.print_exc()
        return "Could not exec %s : %s (%d)\n" % (chroot + Program, strerror, errno)

    return None
    
if __name__ == '__main__':
    error_message = VirtDomainOperation(sys.argv[1:])
    if error_message:
        sys.stderr.write(error_message)
        sys.exit(1)
    else:
        sys.exit(0)
    
