00000058
 "²    # synapsis:	libaray to read and write the domain database
# copyright:	(C) 2001-2005, Hostsharing eG, Hamburg, Germany
# license:	GPL

source /root/etc/config
source /usr/local/lib/hostsharing/pac
source /usr/local/lib/hostsharing/misc

shopt -s extglob

#================================================================================
function doms_by_name() {
# syntax:   doms_by_name DOMAIN-WILDCARD
#
# sample:   doms_by_name '*.de'
#       returns all .de-domains
#
# sample:   doms_by_name '*'
#       returns all domains
#
# synopsis: subroutine queries all domains with a name matching the wildvard

# get options
if [ -n "$THIS_HIVE" ]; then
        aHive="--hive=$THIS_HIVE"
else
        aHive=""
fi
while [ "${1:0:1}" = "-" ]; do
        case $1 in
                --hive|-h)
                        if [ "$2" = '-' -o "$2" = '*' ]; then
                                aHive=
                        else
                                aHive="--hive=$2"
                        fi
                        shift
                        ;;

                *)
                        echo "ERROR: invalid option '$1' to doms_by_name" >&2
                        ;;
        esac
        shift
done

local rc=1
if [ $# -eq 1 ]; then
  hsdb --cmd=doms_by_name $aHive --arg=$1
  rc=$?

  while [ ${rc} -eq 255 ]; do
    sleep 15
    hsdb --cmd=doms_by_name $aHive --arg=$1
    rc=$?
  done
else
  echo >&2 "doms_by_name: Need exactly one argument, got '$*'"
fi

return ${rc}
}

#================================================================================
function dom_get_delegated(){
# syntax:   dom_get_delegated DOMAIN
# sample:   dom_get_delegated foo.bar.baz.hostsharing.net
# synopsis: returns the delegated part of a domain

# get arguments
local dom="$1"

# remove leading elements from the domain until a delegated domain is
# encountered
while [ -n "${dom}" ]; do
    if dom_exists "${dom}"; then
        local dom_hive="$(
            dom_get "${dom}" &&
            pac_get "${DOM_OWNER:0:5}" &&
            echo "${PAC_HOST}"
        )"
        if [ "${dom_hive}" = "${THIS_HIVE}" ]; then
            break
        fi
    fi

    dom="${dom/#*([^.])?(.)}"
done

if [ -n "${dom}" ]; then
    # a delegated domain was found
    echo "${dom}"
    return 0
else
    # no delegated domain found
    return 1
fi
}

#================================================================================
function dom_get_subdomain(){
# syntax:   dom_get_subdomain DOMAIN
# sample:   dom_get_subdomain foo.bar.baz.hostsharing.net
# synopsis: returns the subdomain part of a domain

local dom delegated rc sublength subdomain

# get arguments
dom="$1"

# get the delegated part of the domain
delegated="`dom_get_delegated "${dom}"`"

# check whether a delegated domain was found, i.e. the domain exists,
# and return otherwise
rc=$?
if [ ${rc} -ne 0 ]; then
    return ${rc}
fi

# calculate the length of the subdomain
let sublength=${#dom}-${#delegated}-1

# extract the subdomain if necessary
if [ ${sublength} -gt 0 ]; then
    # the subdomain is non-empty, so extract the subdomain from the domain
    subdomain="${dom:0:${sublength}}"
else
    # the subdomain is empty, i.e. the domain is a delegated domain
    subdomain=""
fi

# return the subdomain
echo "${subdomain}"
return 0
}

#==============================================================================
function dom_clear() {
# syntax:	dom_clear
# sample:	dom_clear
# synopsis:	subroutine clears the variables of a domain record

DOM_DOMAIN=''
DOM_OWNER=''
DOM_IPNUMBER=''
DOM_IPNUMBER_EX=''
return 0
}

#================================================================================
function doms_by_owner() {
# syntax:	doms_by_owner OWNER-REGEXP
#
# sample:	doms_by_owner 'xyz00-.*'
#		returns all delegated domains of packet xyz00
#
# sample:	doms_by_owner 'xyz00.*' 
#		returns all domains of packet xyz00
#
# sample:	doms_by_owner 'xyz00' 
#		returns all domains of packet-owner xyz00
#
# synopsis:	subroutine queries all domains with a name matching the pattern

# get options
if [ -n "$THIS_H