#!/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.
#
#
# This function adds a user to a domain 
#
# Usage:
#
# AddVirtUser [ tpasswd=cleartextpwd | cpasswd=cryptedpwd | passwd ] <domainname> <username> <usersfullname> <usersdiskquota> [<service>=1 ...]
#
# Example:
#
# AddVirtUser passwd myco.com joe 'Joe User' 20 proftpd=1 telnet=0 ssh=1
# Please  enter password: ******
# Please retype password: ******
# 
# or
#
# AddVirtUser tpasswd=cleartext myco.com joe 'Joe User' 20 proftpd=1 telnet=0 ssh=1
#

import getopt
import getpass
import sys
import traceback
from vh3 import virthost
from vh3 import virtutil
from vh3.modules import users
import string
import be_vherrordisp
import random

def usage():
    print "usage: AddVirtUser [ tpasswd=cleartextpwd | cpasswd=cryptedpwd | passwd ] <domainname> <username> <usersfullname> <usersdiskquota> [<service>=1 ...]"

if (len(sys.argv) < 6) or (sys.argv[1] == "--help"): 
    usage()
    sys.exit(1)
else: 
    # checks to see if we are in maintenance mode
    virthost.checkMaintenance()

    status = be_vherrordisp.CLIError.SUCCESS
    status_obj = be_vherrordisp.CLIError()
    try:
        options, args = getopt.getopt(sys.argv[1:],'')
    except:
        usage()
        sys.exit(1)
    pwdtype = string.split(args[0],'=')[0]
    if pwdtype == 'passwd': 
        passwd1 = getpass.getpass("Please  enter password:")
        passwd2 = getpass.getpass("Please retype password:")
        if (passwd1 != passwd2):
            print "Passwords do not match."
            usage()
            sys.exit(1)
        cpasswd = virtutil.crypt_password(passwd1)
    elif pwdtype == 'tpasswd':
        try:
            passwd1 = string.split(args[0],'=')[1]
        except:
            print "Invalid password parameter."
            usage()
            sys.exit(1)
        if not virtutil.valid_passwd(passwd1):
            print "Invalid password."
            usage()
            sys.exit(1)
        cpasswd = virtutil.crypt_password(passwd1)
    elif pwdtype == 'cpasswd':
        try:
            cpasswd = string.split(args[0],'=')[1]
        except:
            print "Invalid password parameter."
            usage()
            sys.exit(1)
        if not virtutil.valid_crypted_passwd(cpasswd):
            print "Invalid crypted password."
            usage()
            sys.exit(1)
    else:
        print "Invalid password parameter."
        usage()
        sys.exit(1)
    siteindex = virthost.get_site_from_anything(string.lower(args[1]))
    username = string.lower(args[2])
    usersfullname = args[3]
    usersdiskquota = args[4]
    if not siteindex:
        print "Domain %s does not exist on this server."% string.lower(args[1])
        sys.exit(1)
    svcoptions = virthost.get_user_toggleable_services(siteindex)
    svcoptionsdict = users.get_services_defaults(siteindex)
    for option in args[5:]:
        optvalues=string.split(option,"=")
        if optvalues[0] in svcoptions:
            svcoptionsdict[optvalues[0]]=optvalues[1]
    ret = []
    try:
        virthost.add_user(ret, siteindex, username, cpasswd, usersfullname, usersdiskquota, svcoptions = svcoptionsdict)
        status = virthost.cli_display_status_list(ret)
    except:
        status = be_vherrordisp.CLIError.ERROR
        print traceback.print_exc()
    sys.exit(status)
