#!/u95/bin/ksh
#			rndc-keyadm(1Mtcp)
#
#rndc-keyadm(1Mtcp) - rndc(1Mtcp) TSIG Key administration utility
#
#-This utility performs administration of TSIG keys, used by the rndc(1Mtcp) and
# DNS/BIND control channel, within the RNDC Secuirity Area of the DNS/BIND
# security key repository which by default is in
# $RNDCKEYADM_RNDC_KEY_REPOSITORY__DIR.
#
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
#SYNOPSIS:
#
#	rndc-keyadm	-a [-c runtime-config-file] [-k plaintext-key | -K]  \
#			[-y keyid ...] [-s server ...] \
#			[-Y keyid] [-S server]
#	rndc-keyadm	-a [-c runtime-config-file] \
#			[-C control-channel-list ...]
#	rndc-keyadm	-d [-c runtime-config-file] \
#			[all-servers | -s server ...]
#	rndc-keyadm	-d [-c runtime-config-file] \
#			[all-controls | -C ip_addr ...]
#	rndc-keyadm	-D [-c runtime-config-file] [-k plaintext-key | -K] \
#			[-Y keyid] [-S server] [AVA-list] \
#			[-C control-channel-list]
#	rndc-keyadm	-L [-c runtime-config-file]
#	rndc-keyadm	[-L]
#
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
#CONVENTIONS:
#
#-Convention  $VARIABLE
#	-References of this type indicate a shell-like substitutable value.
#	 Values for given VARIABLEs are defined in rndc-keyadm.rc(4tcp) and
#	 are referenced in the DEFINITIONS/OPTIONS/OPERATIONS sections.
#
#
#-     -     -     -     -     -     -     -     -     -     -     -     -     -
#
#DEFINITIONS:
#
#-Used in SYNOPSIS:
#
#--AVA-list:
#	-a comma separated list of Attribute-Value-Assertions whose values are
#	 defined in rndc-keyadm(1Mtcp) runtime configuration file whose
#	 default value is defined as $RNDCKEYADM_RC__FILE.
#--all-controls:
#	-a literal value indicating all entries in the DNS/BIND configuration
#	 file's, controls, statement.
#--all-servers:
#	-a literal value indicating all, server, statements in the
#	 rndc(1Mtcp) configuartion file.
#--control-channel-list:
#	-each control-channel-list contains 4 parts
#	 (each part separated by a comma) used to define the, inet, substatement
#	 of the, controls, statement in the DNS/BIND configuration file.
#	 (Refer to BIND 9 Administrative Reference Manual  for details) 
#	-the following is the , controls, Statement Grammar followed by an
#	 explanation of how the 4 parts of the control-channel-list map onto
#	 the, inet, substatement.
#
#	 controls {
#		inet ( ip_addr | * ) [port ip_port ] allow address_match_list
#			keys key_list;
#	 };
#
#	--Part 1:  ip_addr or literal *
#	--Part 2:  ip_port
#	    -Each unique instance of the concatenation of Parts 1 & 2 comprise
#	     a unique instance of the, inet, substatement.
#	    --This will be referred to as the, Control Channel ID.
#	--Part 3:  address_match_list
#	    -Each addres_match_list_element of the address_match_list is
#	     separated by a semi-colon character.
#	    -There must be a list on address_match_list_element specified.
#	    -If no more than one address_match_list_element is specified the
#	     separating semi-colon character is not necessary.
#	    -Do not individually surround types of address_match_list_elements
#	     that require quouting in double qoutes (eg. the acl_name element).
#	     The proper quouting will be automatically determined and applied.
#	--Part 4:  key_list
#	    -Each keyid of the key_list is separated by a semi-colon character.
#	    -There must be a list on keyid specified.
#	    -If no more than one keyid is specified the separating semi-colon
#	     character is not necessary.
#	    -Do not individually surround keyids in double qoutes as this will
#	     automatically applied.
#
#	-control-channel-list example:
#	 "*,,localhost,localhost-rndc-key;127.0.0.1-rndc-key"
#	--Part 1: *
#		-listens on all IP addresses on the given system.
#	--Part 2: NULL value
#		-listens of the default port number which is 953.
#	--Part 3: localhost
#		-allows only IP addresses of all interfaces on the given system.
#	--Part 4: localhost-rndc-key
#		-indicates the list (in this case the list has two elements) of
#		 key_ids, as defined by the DNS/BIND configuration file's
#		 key statement, that is allowed to be used to authenticate
#		 commands and responses given over the control channel.
#--keyid:
#	-indicates a key identification (i.e. a short-hand reference) of the
#	 shared secret key between rndc(1Mtcp) and DNS/BIND's control channel(s)
#	 for the specified name server(s).
#--ip_addr:
#	-IP address in either
#		-IPV4 or IPV6 notation.
#		-'*' asterisk character a wild card matching all IP addresses.
#--plaintext-key:
#	-indicates the plain text of the shared secret, between rndc(1Mtcp) and
#	 DNS/BIND's control channel(s) for specified name server(s), that will
#	 be encoded in base-64 via mimencode(1).
#--runtime-config-file:
#	-path of an alternate runtime configuration file.
#	-the defualt value is defined as $RNDCKEYADM_RC__FILE.
#--server:
#	-indicates a name server and can be specified in the following formats
#		-name server's FQDN hostname (without the trailing dot, i.e
#		 the "root" dot)
#		-name server's IP address
#
#
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
#OPTIONS:
#
#-Options -a  -d  -D  and  -L are mutually exclusive.
#
#-- -a
#	-Generates or updates
#-- -d
#	-Deletes
#
#	--the rndc.conf(4tcp) style statements used by rndc(1Mtcp)
#	  for each defined
#	  ${RNDCKEYADM_RNDC_KEY_REPOSITORY_DIR}/NAMESERVER.D/NAMESERVER_CONFIG_FILE
#	  which are included by $RNDCKEYADM_RNDC_CONFIG__FILE.
#	--the rndc(1Mtcp) related statements in named.conf(4tcp)
#	  (i.e. controls, and key) for each defined
#	  ${RNDCKEYADM_RNDC_KEY_REPOSITORY_DIR}/NAMESERVER.D/NAMESERVER_INCLUDE_FILE
#	  which are included by $RNDCKEYADM_RNDC_INCLUDE__FILE.
#
#
#-- -D
#	-Defines/Redefines values and default values that are defined in
#	 $RNDCKEYADM_RC__FILE.
#
#-- -L
#	-Lists values and default values that are defined in
#	 $RNDCKEYADM_RC__FILE.
#
#-     -     -     -     -     -     -     -     -     -     -     -     -     -
#
#-Each of the following options takes precedence over certain variables either
# defined in rndc-keyadm(1Mtcp) or rndc-keyadm.rc(4tcp).
#--The set of variables a particular options takes precedence over is listed in
#  the particular option's description.
#-Each of the following opitons has an affect on certain rndc.conf(4tcp) and/or
# named.conf(4tcp) statements and substatements.
#--The set of statements and substatements affected by a particular option is
#  listed in the particular option's description.
#
#-- -c runtime-config-file
#	-Definition:
#	--The value of runtime-config-file is the path of an alternate
#	  rndc-keyadm.rc(4tcp) type file.
#	-Effects rndc-keyadm(1Mtcp) Variable:
#	-- RNDCKEYADM_RC_FILE
#	-Effects rndc.conf(4tcp) statements/substatements:
#	-- NONE
#	-Effects named.conf(4tcp) statements/substatements:
#	-- NONE
#
#-- -C control-channel-list | ip_addr
#	-Effects rndc-keyadm.rc(4tcp) Variable:
#	--RNDCKEYADM_CONTROLS_DEFAULT_IPADDR
#	--RNDCKEYADM_CONTROLS_DEFAULT_IPPORT
#	--RNDCKEYADM_CONTROLS_DEFAULT_ALLOW
#	--RNDCKEYADM_CONTROLS_DEFAULT_KEYS
#	-Effects rndc.conf(4tcp) statements/substatements:
#	-- NONE
#	-Effects named.conf(4tcp) statements/substatements:
#	-- controls inet
#
#-- -k plaintext-key
#	-Effects rndc-keyadm.rc(4tcp) Variable:
#	--RNDCKEYADM_DEFAULT_KEYGEN
#	--RNDCKEYADM_DEFAULT_PLAINTEXT
#	-Effects rndc.conf(4tcp) statements/substatements:
#	-- key secret
#	-Effects named.conf(4tcp) statements/substatements:
#	-- key secret
#
#-- -K
#	-Effects rndc-keyadm.rc(4tcp) Variable:
#	--RNDCKEYADM_DEFAULT_KEYGEN
#	-Effects rndc.conf(4tcp) statements/substatements:
#	-- key secret
#	-Effects named.conf(4tcp) statements/substatements:
#	-- key secret
#
#-- -s server
#	-Indirectly Effects rndc-keyadm.rc(4tcp) Variable:
#	--RNDCKEYADM_OPTIONS_DEFAULT_SERVER
#	-Effects rndc.conf(4tcp) statements/substatements:
#	-- server
#	-Indirectly Effects rndc.conf(4tcp) statements/substatements:
#	-- options default-server
#	-Effects named.conf(4tcp) statements/substatements:
#	-- NONE
#
#-- -S server
#	-Effects rndc-keyadm.rc(4tcp) Variable:
#	--RNDCKEYADM_OPTIONS_DEFAULT_SERVER
#	-Effects rndc.conf(4tcp) statements/substatements:
#	-- options default-server
#	-- server
#	-Effects named.conf(4tcp) statements/substatements:
#	-- NONE
#
#-- -y keyid
#	-Indirectly Effects rndc-keyadm.rc(4tcp) Variable:
#	--RNDCKEYADM_OPTIONS_DEFAULT_KEYID
#	-Effects rndc.conf(4tcp) statements/substatements:
#	-- key
#	-Effects named.conf(4tcp) statements/substatements:
#	-- key
#
#-- -Y keyid
#	-Effects rndc-keyadm.rc(4tcp) Variable:
#	--RNDCKEYADM_OPTIONS_DEFAULT_KEYID
#	-Effects rndc.conf(4tcp) statements/substatements:
#	-- options default-key
#	-- key
#	-Effects named.conf(4tcp) statements/substatements:
#	-- key
#
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
#OPERATIONS:
#
#-Invocation form:
#
# rndc-keyadm	-a [-c runtime-config-file] [-k plaintext-key | -K]  \
#		[-y keyid ...] [-s server ...] [-Y keyid] [-S server] \
#		[-C control-channel-list ...]
#
#--Generates or Updates the following files base on the information in the
#  rndc-keyadm.rc(4tcp) type file $RNDCKEYADM_RC__FILE and the
#  Command Line Options:  
#---rndc.conf(4tcp) type file $RNDCKEYADM_RNDC_CONFIG__FILE
#---named.conf(4tcp) type file $RNDCKEYADM_RNDC_INCLUDE__FILE
#
#---$RNDCKEYADM_RNDC_INFO__FILE
#	-used in generation of $RNDCKEYADM_RNDC_INCLUDE__FILE
#---$RNDCKEYADM_RNDC_KEY_REPOSITORY__DIR/NAMESERVER.D/NAMESERVER_INFO_FILE
#	-used in generation of $RNDCKEYADM_RNDC_INCLUDE__FILE
#	-used in generation of $RNDCKEYADM_RNDC_CONFIG__FILE
#
#--If rndc-keyadm -a is invoked without any other command line options 
#  rndc-keyadm generates/updates the files listed above for the
#---Default Server as defined in the $RNDCKEYADM_RC__FILE under the following
#    conditions:
#	-If the NAMESERVER_INFO_FILE for the Default Server as defined in the
#	 $RNDCKEYADM_RC__FILE does not exist
#	-If the currently defined Default Server (as defined in
#	 $RNDCKEYADM_RNDC_CONFIG__FILE) has any one or more of the
#	 following attributes that differ from the attriubtes defined in
#	 $RNDCKEYADM_RC__FILE
#	--$RNDCKEYADM_OPTIONS_DEFAULT_SERVER
#	--$RNDCKEYADM_OPTIONS_DEFAULT_KEYID
#    -NOTE:
#	-This invocation will change a Default Server previously set via the
#	 '-S' option is any of the attributes described above differ.
#---Default Key ID as defined in the $RNDCKEYADM_RC__FILE under the following
#    conditions:
#	-If the currently defined Default Key ID (as defined in
#	 $RNDCKEYADM_RNDC_CONFIG__FILE) has any one or more of the
#	 following attributes that differ from the attriubtes defined in
#	 $RNDCKEYADM_RC__FILE
#	--$RNDCKEYADM_OPTIONS_DEFAULT_KEYID
#	--$RNDCKEYADM_DEFAULT_KEYALGORITHM
#	--$RNDCKEYADM_DEFAULT_KEYGEN
#	--$RNDCKEYADM_DEFAULT_PLAINTEXT
#    -NOTE:
#	-This invocation will change a Default Server previously set via the
#	 '-Y' option is any of the attributes described above differ.
#
#--If the -s option is specifed and the -S option is not, the files listed
#  above for the Default Server as defined in the $RNDCKEYADM_RC__FILE are
#  not generated/updated.
#--NOTE:
#	-This does not effect the definition of the Default Server if it is
#	 already defined in the $RNDCKEYAMD_CONFIG__FILE
#
#--Each time the -s or -S option is encountered,  its associated
#  keyid definition is from
#---the most recently defined -y or -Y option respectively
#---the associated Default Values as defined in $RMDCKEYADM_RC__FILE
#   (eg.
#	Default Value if -y not specified is RNDCKEYADM_OPTIONS_DEFAULT_KEYID
#   )
#
#--Each time the -y or -Y option is encountered,  its associated secret key
#  generation disposition and definition, is either from
#---a previously encountered -k|-K
#---the associated Default Values as defined in $RMDCKEYADM_RC__FILE
#   (eg.
#	Default Value if -k | -K not specified is
#	$RNDCKEYADM_DEFAULT_KEYGEN
#	$RNDCKEYADM_DEFAULT_PLAINTEXT
#   )
#
#--If the -C option is specified,  the Default, controls inet, substatement,
#  as indicated in $RMDCKEYAMD_RC__FILE, is not generated/updated unless the
#  Control Channel ID specified by -C is the same as the
#  Default Control Channel ID as derived in $RMDCKEYAMD_RC__FILE.
#--NOTE:
#	-IF the -C option has not specified the Default Control Channel ID,
#	 this does not effect the definition the Default,  controls inet,
#	 substatement that is defined in the $RNDCKEYAMD_INCLUDE__FILE.
#-     -     -     -     -     -     -     -     -     -     -     -     -     -
#-     -     -     -     -     -     -     -     -     -     -     -     -     -
#-     -     -     -     -     -     -     -     -     -     -     -     -     -
#-     -     -     -     -     -     -     -     -     -     -     -     -     -
#


################################################################################
#				FUNCTION DEFINITIONS
################################################################################


#   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #
#
#-rndckeyadm_parseOptions()
#--rndckeyadm_parseOptions_RC()
#
rndckeyadm_parseOptions () {
    DEBUG "IN rndckeyadm_parseOptions: ENTER" 

    DEBUG "NUMBER of positional paramaters == ${#*}"

    ############################################################################
    #
    #-Initialize all the variables used to process the command line options.
    #-Option/Command Argument identity/format. 
    #
    OPT_a=([OPT_a]="" [ID]="-a")
    OPT_c=([OPT_c]="" [ID]="-c")
    OPT_d=([OPT_d]="" [ID]="-d")
    OPT_k=([OPT_k]="" [ID]="-k")
    OPT_s=([OPT_s]="" [ID]="-s")
    OPT_y=([OPT_y]="" [ID]="-y")
    OPT_C=([OPT_C]="" [ID]="-C")
    OPT_D=([OPT_D]="" [ID]="-D")
    OPT_K=([OPT_K]="" [ID]="-K")
    OPT_L=([OPT_L]="" [ID]="-L")
    OPT_S=([OPT_S]="" [ID]="-S")
    OPT_Y=([OPT_Y]="" [ID]="-Y")
    OPTX_s=([OPTX_s]="" [ID]="all-servers")
    OPTX_C=([OPTX_C]="" [ID]="all-controls")
    OPTX_D_AVALIST=([OPT_D_AVALIST]="" [ID]="")

    OPT_TODO=([OPT_TODO]="")


    ############################################################################
    #
    #-OPTLIST
    #	-List of all internally referenced representations of the command line
    #    options.
    #
    OPTLIST="\
	OPT_a \
	OPT_c \
	OPT_d \
	OPT_k \
	OPT_s \
	OPT_y \
	OPT_C \
	OPT_D \
	OPT_K \
	OPT_L \
	OPT_S \
	OPT_Y \
	OPTX_s \
	OPTX_C \
	OPTX_D_AVALIST \
    "

    ############################################################################
    #
    #-Each of the following OPTVALID arrays define the set of options that
    # are valid for a given Operation.
    #--Each rndc-keyadm Operation is identified and indexed by its
    #  main/anchor option.
    #--Options that are mutually exclusive within an Operation are joined
    #  together with a colon character.
    #
    typeset -A OPTVALID
    OPTVALID[OPT_a]="\
	OPT_a \
	OPT_c \
	OPT_k:OPT_K \
	OPT_s \
	OPT_y \
	OPT_C \
	OPT_K:OPT_k \
	OPT_S \
	OPT_Y \
    "
    OPTVALID[OPT_d]="\
	OPT_c \
	OPT_d \
	OPT_s:OPTX_s \
	OPT_C:OPTX_C \
	OPTX_s:OPT_s \
	OPTX_C:OPT_C \
    "
    OPTVALID[OPT_D]="\
	OPT_c \
	OPT_k:OPT_K \
	OPT_C \
	OPT_D \
	OPT_K:OPT_k \
	OPT_S \
	OPT_Y \
	OPTX_D_AVALIST \
    "
    OPTVALID[OPT_L]="\
	OPT_c \
	OPT_L \
    "

    ############################################################################
    #
    #-Each of the following OPTRC arrays, indexed by the option,
    # define a set of rndc-keyadm.rc(4tcp) type variables that are effected
    # for the given OPTRC array entry.
    #--If a given option does not effect any rndc-keyadm.rc(4tcp) type variables
    #  the associated OPTRC array index value is set to NULL.
    #--Entries defined in the OPTRC arrays are used as indexes into a given
    #  Option array.
    #	-(eg.
    #	 OPTRC[OPT_c]="RNDCKEYADM_RC__FILE"
    #	 OPT_c[RNDCKEYADM_RC__FILE]="The value specified for options -c"
    #
    typeset -A OPTRC
    OPTRC[OPT_a]=""
    OPTRC[OPT_c]="RNDCKEYADM_RC__FILE"
    OPTRC[OPT_d]=""
    OPTRC[OPT_k]="\
	RNDCKEYADM_DEFAULT_KEYGEN \
	RNDCKEYADM_DEFAULT_PLAINTEXT \
    "
    OPTRC[OPT_s]=""
    OPTRC[OPT_y]=""
    OPTRC[OPT_C]="\
	RNDCKEYADM_CONTROLS_DEFAULT_IPADDR \
	RNDCKEYADM_CONTROLS_DEFAULT_IPPORT \
	RNDCKEYADM_CONTROLS_DEFAULT_ALLOW \
	RNDCKEYADM_CONTROLS_DEFAULT_KEYS \
    "
    OPTRC[OPT_D]=""
    OPTRC[OPT_K]="\
	RNDCKEYADM_DEFAULT_KEYGEN \
    "
    OPTRC[OPT_L]=""
    OPTRC[OPT_S]="\
	RNDCKEYADM_OPTIONS_DEFAULT_SERVER \
    "
    OPTRC[OPT_Y]="\
	RNDCKEYADM_OPTIONS_DEFAULT_KEYID \
    "
    OPTRC[OPTX_s]=""
    OPTRC[OPTX_C]=""
    OPTRC[OPTX_D_AVALIST]=""

    OPTRC[OPT_TODO]=""


    ############################################################################
    #
    #-SERVERSET is an indexed array contains the information that the
    # rndc-keyadm(1Mtcp) uses when performing one of its Operations.
    #-Each index entry is comprised of 3 fields.
    #
    #-FIELD 1: Characteristics Field
    #	-Entries indicate particular chraracteristics of the given indexed
    #	 entry which are used by rndc-keyadm(1Mtcp) when performing one of
    #	 its Operations.
    #	-The set of Characteristics along with their definitions are as follows:
    #	--DEFAULT
    #		-Indicates the indexed entry is to become the Default Server for
    #		 rndc(1Mtcp).
    #	--STANDARD
    #		-Indicates the indexed entry is a normal server entry.
    #		-NOTE: this is the "default" characteristic thus a Null entry
    #		 entry for this field indicates a STANDARD entry.
    #	--IGNORE
    #		-Indicates the indexed entry is to be ignored.
    #		-This characteristic is only set when performing one of the
    #		 anchor Operations functionality  based on some criteria in
    #		 the anchor Operation.
    #		 (eg.
    #			-add Operation determines that information in the
    #			 SERVERSET entry already exists in the
    #			 RNDC.d Security Area and no changes are needed.
    #		 )
    #
    #-FIELD 2-3: Server and Key ID related fields
    #	-Fields 2-3 describe how to generate the server statements for
    #	 the rndc.conf(4tcp) type file.
    #-FIELD 2: NAMESERVER FIELD
    #	-Values that are directly specified are either a server's
    #	 FQDN (without the trailing dot) or IP Address format.
    #	-Typically indirect entry is variable RNDCKEYADM_OPTIONS_DEFAULT_SERVER
    #-FIELD 3: KEYID FIELD
    #	-Values that are directly specified are key identifications, in
    #    domain name format, of the shared secret key.
    #	-Typically indirect entry is variable RNDCKEYADM_OPTIONS_DEFAULT_KEYID
    #
    #		  
    #-A SERVERSET indexed entry is created each time a -s or -S option
    # is processed.
    #
    #
    #		  
    #-To assist in the generation of SERVERSET entries the following variables
    # respectively keep track of the current values of the following SERVERSET
    # fields:
    #	-CURRENT_CHAR			-FIELD 1: CHARACTERISTIC  
    #	-CURRENT_NAMESERVER		-FIELD 2: NAMESERVER  
    #	-CURRENT_KEYID			-FIELD 3: KEYID  
    #
    #
    #-SERVERSET is initialized to define the values setup in the
    # rndc-keyadm.rc(4tcp) type file.
    #

    typeset -i SERVERSETindx=0

    SERVERSET[0]="\
	\"DEFAULT\" \
	\"\\\"\\\$RNDCKEYADM_OPTIONS_DEFAULT_SERVER\\\"\" \
	\"\\\"\\\$RNDCKEYADM_OPTIONS_DEFAULT_KEYID\\\"\" \
    "
    DEBUG "IN rndckeyadm_parseOptions: SERVERSET[0] == \"${SERVERSET[0]}\""
    CURRENT_CHAR="\"DEFAULT\""
    CURRENT_NAMESERVER="\"\\\"\\\$RNDCKEYADM_OPTIONS_DEFAULT_SERVER\\\"\""
    CURRENT_KEYID="\"\\\"\\\$RNDCKEYADM_OPTIONS_DEFAULT_KEYID\\\"\""

    DEBUG "IN rndckeyadm_parseOptions: CURRENT_CHAR == \"$CURRENT_CHAR\""
    DEBUG "IN rndckeyadm_parseOptions: CURRENT_NAMESERVER == \"$CURRENT_NAMESERVER\""
    DEBUG "IN rndckeyadm_parseOptions: CURRENT_KEYID == \"$CURRENT_KEYID\""


    ############################################################################
    #
    #-KEYSET is an indexed array contains the information that the
    # rndc-keyadm(1Mtcp) uses when performing one of its Operations.
    #-Each index entry is comprised of 5 fields.
    #
    #-FIELD 1: Characteristics Field
    #	-Entries indicate particular chraracteristics of the given indexed
    #	 entry which are used by rndc-keyadm(1Mtcp) when performing one of
    #	 its Operations.
    #	-The set of Characteristics along with their definitions are as follows:
    #	--DEFAULT
    #		-Indicates the indexed entry is to become the Default Key for
    #		 rndc(1Mtcp).
    #	--STANDARD
    #		-Indicates the indexed entry is a regular key entry.
    #		-NOTE: this is the "default" characteristic thus a Null entry
    #		 entry for this field indicates a STANDARD entry.
    #	--IGNORE
    #		-Indicates the indexed entry is to be ignored.
    #		-This characteristic is only set when performing one of the
    #		 anchor Operations functionality  based on some criteria in
    #		 the anchor Operation.
    #		 (eg.
    #			-add Operation determines that information in the
    #			 KEYSET entry already exists in the
    #			 RNDC.d Security Area and no changes are needed.
    #		 )
    #
    #-FIELD 2-5: Server and Key related fields
    #	-Fields 2-5 describe how to generate the key statements for
    #	 the rndc.conf(4tcp) type file and the key statement for the
    #	 named.conf(4tcp) type file.
    #-FIELD 2: KEYID FIELD
    #	-Values that are directly specified are key identifications, in
    #    domain name format, of the shared secret key.
    #	-Typically indirect entry is variable RNDCKEYADM_OPTIONS_DEFAULT_KEYID
    #-FIELD 3: KEYGEN FIELD
    #	-Values that are directly specified are the methods (i.e. how the
    #	 secret TSIG is genernated) the secret key is generated
    #	-Typically indirect entry is variable RNDCKEYADM_DEFAULT_KEYGEN
    #-FIELD 4: PLAINTEXT FIELD
    #	-Values that are directly specified are the "Plain Text" used when
    #	 using the, mimencode, method for generating the secret key.
    #	-Typically indirect entry is variable RNDCKEYADM_DEFAULT_PLAINTEXT
    #-FIELD 5: KEY ALGORITHM FIELD
    #	-Values that are directly specified are the algorithm by which the
    #	 TSIG key is generated.
    #	-Typically indirect entry is variable RNDCKEYADM_DEFAULT_KEYALGORITHM
    #
    #		  
    #-A KEYSET indexed entry is created each time a -y or -Y option
    # is processed.
    #
    #
    #		  
    #-To assist in the generation of KEYSET entries the following variables
    # respectively keep track of the current values of the following KEYSET
    # fields:
    #	-CURRENT_CHAR			-FIELD 1: CHARACTERISTIC  
    #	-CURRENT_KEYID			-FIELD 2: KEYID  
    #	-CURRENT_KEYGEN			-FIELD 3: KEYGEN  
    #	-CURRENT_PLAINTEXT		-FIELD 4: PLAINTEXT  
    #	-CURRENT_KEYALGORITHM		-FIELD 5: KEYALGORITHM  
    #
    #	-CURRENT_DEFAULT_KEYID		-FIELD 2: KEYID for KEYSET[0]  
    #
    #-KEYSET is initialized to define the values setup in the
    # rndc-keyadm.rc(4tcp) type file.
    #

    typeset -i KEYSETindx=0

    KEYSET[0]="\
	\"DEFAULT\" \
	\"\\\"\\\$RNDCKEYADM_OPTIONS_DEFAULT_KEYID\\\"\" \
	\"\\\"\\\$RNDCKEYADM_DEFAULT_KEYGEN\\\"\" \
	\"\\\"\\\$RNDCKEYADM_DEFAULT_PLAINTEXT\\\"\" \
	\"\\\"\\\$RNDCKEYADM_DEFAULT_KEYALGORITHM\\\"\" \
    "
    DEBUG "IN rndckeyadm_parseOptions: KEYSET[0] == \"${KEYSET[0]}\""
    CURRENT_CHAR="\"DEFAULT\""
    CURRENT_KEYID="\"\\\"\\\$RNDCKEYADM_OPTIONS_DEFAULT_KEYID\\\"\""
    CURRENT_DEFAULT_KEYID="\"\\\"\\\$RNDCKEYADM_OPTIONS_DEFAULT_KEYID\\\"\""
    CURRENT_KEYGEN="\"\\\"\\\$RNDCKEYADM_DEFAULT_KEYGEN\\\"\""
    CURRENT_PLAINTEXT="\"\\\"\\\$RNDCKEYADM_DEFAULT_PLAINTEXT\\\"\""
    CURRENT_KEYALGORITHM="\"\\\"\\\$RNDCKEYADM_DEFAULT_KEYALGORITHM\\\"\""

    DEBUG "IN rndckeyadm_parseOptions: CURRENT_CHAR == \"$CURRENT_CHAR\""
    DEBUG "IN rndckeyadm_parseOptions: CURRENT_KEYID == \"$CURRENT_KEYID\""
    DEBUG "IN rndckeyadm_parseOptions: CURRENT_DEFAULT_KEYID == \"$CURRENT_DEFAULT_KEYID\""
    DEBUG "IN rndckeyadm_parseOptions: CURRENT_KEYGEN == \"$CURRENT_KEYGEN\""
    DEBUG "IN rndckeyadm_parseOptions: CURRENT_PLAINTEXT == \"$CURRENT_PLAINTEXT\""
    DEBUG "IN rndckeyadm_parseOptions: CURRENT_KEYALGORITHM == \"$CURRENT_KEYALGORITHM\""




    ############################################################################
    #
    #-CONTROLSSET is an indexed array contains the information that the
    # rndc-keyadm(1Mtcp) uses when performing one of its Operations.
    #-Each index entry is comprised of 6 fields.
    #
    #-FIELD 1: Characteristics Field
    #	-Entries indicate particular chraracteristics of the given indexed
    #	 entry which are used by rndc-keyadm(1Mtcp) when performing one of
    #	 its Operations.
    #	-The set of Characteristics along with their definitions are as follows:
    #	--DEFAULT
    #		-Indicates the indexed entry is to become the Default, 
    #		 controls inet, substatement.
    #		-NOTE: there is a minimum of one, controls inet, which is
    #		 indicated by this Characteristic Value.
    #		 Therefore there is at least on CONTROLSSET entry with a
    #		 DEFAULT Characteristic.
    #	--STANDARD
    #		-Indication for all other, controls inet, substatements.
    #		-NOTE: this is the "default" characteristic thus a Null entry
    #		 entry for this field indicates a STANDARD entry.
    #	--IGNORE
    #		-Indicates the indexed entry is to be ignored.
    #		-This characteristic is only set when performing one of the
    #		 anchor Operations functionality  based on some criteria in
    #		 the anchor Operation.
    #		 (eg.
    #			-add Operation determines that information in the
    #			 COTROLSSET entry already exists in the
    #			 RNDC.d Security Area and no changes are needed.
    #		 )
    #
    #-FIELD 2-6: controls inet substatement related fields
    #	-Fields 2-6 describe how to generate the, controls inet, substatement
    #	 for the named.conf(4tcp) type file.
    #-FIELD 2: ID FIELD
    #	-Values are are uniquely generated ID to distiguish one entry from
    #	 another entry.
    #	--It is comprised of the IPADDR and IPPORT part of the -C option's 
    #	  Option Argument.
    #-FIELD 3: IPADDR FIELD
    #	-Values that are directly specified are either
    #	--an IP address on the system in IP Address format.
    #	--the * character indicating all IP addresses on the system.
    #	-Typically indirect entry is variable RNDCKEYADM_CONTOLS_DEFAULT_IPADDR
    #-FIELD 4: IPPORT FIELD
    #	-Values that are directly specified are IP ports in IP Port format.
    #	-Typically indirect entry is variable RNDCKEYADM_CONTOLS_DEFAULT_IPPORT
    #-FIELD 5: ALLOW FIELD
    #	-Values that are directly specified control connections to the IP Addr
    #	 and are in, address_match_list, format
    #	 (Refer to DNS/BIND configuration file Grammar for details on
    #	 address_match_list).
    #	-Typically indirect entry is variable RNDCKEYADM_CONTOLS_DEFAULT_ALLOW
    #-FIELD 6: KEYS FIELD
    #	-Values that are directly specified are list of Key IDs and are in,
    #	 key-list, format.
    #	 (Refer to DNS/BIND configuration file Grammar for details on
    #	 key-lists).
    #	-Typically indirect entry is variable RNDCKEYADM_CONTOLS_DEFAULT_KEYS
    #		  
    #-A CONTROLSSET indexed entry is created each time a -C option is processed.
    #
    #
    #		  
    #-To assist in the generation of CONTROLSSET entries the following variables
    # respectively keep track of the current values of the following CONTROLSSET
    # fields:
    #	-CURRENT_CHAR			-FIELD 1: CHARACTERISTIC  
    #	-CURRENT_ID			-FIELD 2: ID  
    #	-CURRENT_IPADDR			-FIELD 3: IPADDR  
    #	-CURRENT_IPPORT			-FIELD 4: IPPORT  
    #	-CURRENT_ALLOW			-FIELD 5: ALLOW  
    #	-CURRENT_KEYS			-FIELD 6: KEYS  
    #
    #
    #-CONTROLSSET is initialized to define the values setup in the
    # rndc-keyadm.rc(4tcp) type file.
    #

    typeset -i CONTROLSSETindx=0


##JPF##	\"\\\"\\\$RNDCKEYADM_CONTROLS_DEFAULT_IPADDR,\\\$RNDCKEYADM_CONTROLS_DEFAULT_IPPORT,\\\$RNDCKEYADM_CONTROLS_DEFAULT_ALLOW,\\\$RNDCKEYADM_CONTROLS_DEFAULT_KEYS\\\"\" \
    CONTROLSSET[0]="\
	\"DEFAULT\" \
	\"\\\"\\\$RNDCKEYADM_CONTROLS_DEFAULT_IPADDR,\\\$RNDCKEYADM_CONTROLS_DEFAULT_IPPORT\\\"\" \
	\"\\\"\\\$RNDCKEYADM_CONTROLS_DEFAULT_IPADDR\\\"\" \
	\"\\\"\\\$RNDCKEYADM_CONTROLS_DEFAULT_IPPORT\\\"\" \
	\"\\\"\\\$RNDCKEYADM_CONTROLS_DEFAULT_ALLOW\\\"\" \
	\"\\\"\\\$RNDCKEYADM_CONTROLS_DEFAULT_KEYS\\\"\" \
    "
    DEBUG "IN rndckeyadm_parseOptions: CONTROLSSET[0] == \"${CONTROLSSET[0]}\""
    CURRENT_CHAR="\"DEFAULT\""
##JPF##    CURRENT_ID="\"\\\"\\\$RNDCKEYADM_CONTROLS_DEFAULT_IPADDR,\\\$RNDCKEYADM_CONTROLS_DEFAULT_IPPORT,\\\$RNDCKEYADM_CONTROLS_DEFAULT_ALLOW,\\\$RNDCKEYADM_CONTROLS_DEFAULT_KEYS\\\"\""
    CURRENT_ID="\"\\\"\\\$RNDCKEYADM_CONTROLS_DEFAULT_IPADDR,\\\$RNDCKEYADM_CONTROLS_DEFAULT_IPPORT\\\"\""
    CURRENT_IPADDR="\"\\\"\\\$RNDCKEYADM_CONTROLS_DEFAULT_IPADDR\\\"\""
    CURRENT_IPPORT="\"\\\"\\\$RNDCKEYADM_CONTROLS_DEFAULT_IPPORT\\\"\""
    CURRENT_ALLOW="\"\\\"\\\$RNDCKEYADM_CONTROLS_DEFAULT_ALLOW\\\"\""
    CURRENT_KEYS="\"\\\"\\\$RNDCKEYADM_CONTROLS_DEFAULT_KEYS\\\"\""

    DEBUG "IN rndckeyadm_parseOptions: CURRENT_CHAR == \"$CURRENT_CHAR\""
    DEBUG "IN rndckeyadm_parseOptions: CURRENT_ID == \"$CURRENT_ID\""
    DEBUG "IN rndckeyadm_parseOptions: CURRENT_IPADDR == \"$CURRENT_IPADDR\""
    DEBUG "IN rndckeyadm_parseOptions: CURRENT_IPPORT == \"$CURRENT_IPPORT\""
    DEBUG "IN rndckeyadm_parseOptions: CURRENT_ALLOW == \"$CURRENT_ALLOW\""
    DEBUG "IN rndckeyadm_parseOptions: CURRENT_KEYS == \"$CURRENT_KEYS\""


    ############################################################################
    #
    #-ARGLIST
    #	-List of valid command line options for rndc-keyadm(1Mtcp)
    #
    #-OPTREQURIED
    #	-List of the mutually exclusive anchor options.
    #
    #-OPTDETECT
    #	-List of options detected on the command line
    #
    #-OPTMUTEX and OPTMUTEX_OPT
    #	-Respectively List and Array used for performing mutual exclusion
    #	 checks for the anchor options:
    #		-a
    #		-d
    #		-D
    #		-L
    #
    #-CMDARGS and CMDARGSindx
    #	-Respectively Array and Array index used to save command line arguments
    #	 which are processed after all the command line options have been
    #	 detected.
    #
    #-NUMCMDLINEPARAMS
    #	-Keeps track of the number of Command Line Paramaters left to process.
    #


    ARGLIST=":ac:dk:s:y:C:DKLS:Y:"
    OPTREQUIRED="${OPT_a[ID]} | ${OPT_d[ID]} | ${OPT_D[ID]} | ${OPT_L[ID]}"
    OPTMUTEX=""
    typeset -A OPTMUTEX_OPT
    set -A CMDARGS
    typeset -i CMDARGSindx=0
    typeset -i NUMCMDLINEPARAMS=${#*}

    while true
    do
	DEBUG "\nPROCESSING ARGS == \"$@\""
	while getopts $ARGLIST OPT
	do
	    eval OPTCUR=\`echo \"\\\$`expr $OPTIND - 1`\"\` 
	    case $OPT in
	    c|k|s|y|C|S|Y)
		eval OPTCUR=\`echo \"\\\$`expr $OPTIND - 2`\"\` 
		;;
	    esac
	    DEBUG "OPTCUR == $OPTCUR"
	    DEBUG "OPT == $OPT : \c"
	    DEBUG "OPTARG == $OPTARG : \c"
	    DEBUG "OPTIND == $OPTIND : \c"
	    DEBUG "OPTCUR == $OPTCUR"

	    #
	    #-Save representation of option detection from options listed in
	    # OPTLIST but only on the first detection.
	    # 
	    if [[ ($OPTLIST == ?(*OPT_$OPT*)) ]]
	    then
		if [[ (-z $OPTDETECT) || \
		 ("$OPTDETECT" != ?(*OPT_$OPT*)) ]]
		then
		    OPTDETECT="$OPTDETECT OPT_$OPT "
		fi
	    fi

	    case $OPT in
	    a|d|D|L)
		if [[ (-n $OPTMUTEX) && ($OPTMUTEX != ?(OPT_$OPT)) ]]
		then
		    printf "Option  %s  and  %s  are mutually exclusive\n" \
		     ${OPTMUTEX_OPT[$OPTMUTEX]} $OPTCUR 
		    rndckeyadm_usage # No Return
		fi
		OPTMUTEX="OPT_$OPT"
		OPTMUTEX_OPT[$OPTMUTEX]="$OPTCUR"
		;;
	    c)
		if [[ ! -a $OPTARG ]]
		then
		    printf \
		     "Option  %s  runtime-config-file  %s  does not exist\n" \
		     $OPTCUR $OPTARG
		    exit 3
		fi
	 	eval OPT_$OPT[OPT_$OPT]=\"\$OPTARG\"
		;;
	    d)
		;;
	    k)
		#
		#-Setup OPT_k array to process OPTRC[OPT_k]
		#
		eval OPT_$OPT[OPT_$OPT]=\"mimencode \\\"\$OPTARG\\\"\"
		CURRENT_PLAINTEXT="\"\\\"$OPTARG\\\"\""
		CURRENT_KEYGEN="\"\\\"mimencode\\\"\""
		;;
	    s)
		#
		#-OPT_s array is set to the OPTARG of the most recent OPT_s
		#
		eval OPT_$OPT[OPT_$OPT]=\"\$OPTARG\"

		#
		#-Setup the next SERVERSET index entry from
		# the settings in CURRENT_* after performing the following:
		#--CURRENT_CHAR="\"STANDARD\""
		#--CURRENT_NAMESERVER="\"$OPTARG\""
		#--Increment SERVERSETindx
		#
		CURRENT_CHAR="\"\\\"STANDARD\\\"\""
		CURRENT_NAMESERVER="\"\\\"$OPTARG\\\"\""
		(( SERVERSETindx++ ))
		SERVERSET[$SERVERSETindx]="\
		 $CURRENT_CHAR \
		 $CURRENT_NAMESERVER \
		 $CURRENT_KEYID \
		 $CURRENT_KEYGEN \
		 $CURRENT_PLAINTEXT \
		 $CURRENT_KEYALGORITHM \
		"
		DEBUG "IN rndckeyadm_parseOptions: -s  SERVERSET[$SERVERSETindx] == \" ${SERVERSET[$SERVERSETindx]} \""

		#
		#-TODO: add checking for server in either FQDN (without the
		# trailing doe) or IP Addr format.
		#
		;;
	    y)
		#
		#-OPT_y array is set to the OPTARG of the most recent OPT_y
		#
		eval OPT_$OPT[OPT_$OPT]=\"\$OPTARG\"

		#
		#-Setup the next KEYSET index entry from
		# the settings in CURRENT_* after performing the following:
		#--CURRENT_CHAR="\"STANDARD\""
		#--CURRENT_KEYID="\"$OPTARG\""
		#--Increment KEYSETindx
		#
		CURRENT_CHAR="\"\\\"STANDARD\\\"\""
		CURRENT_KEYID="\"\\\"$OPTARG\\\"\""
		(( KEYSETindx++ ))
		KEYSET[$KEYSETindx]="\
		 $CURRENT_CHAR \
		 $CURRENT_KEYID \
		 $CURRENT_KEYGEN \
		 $CURRENT_PLAINTEXT \
		 $CURRENT_KEYALGORITHM \
		"
		DEBUG "IN rndckeyadm_parseOptions: -y  KEYSET[$KEYSETindx] == \" ${KEYSET[$KEYSETindx]} \""

		#
		#-TODO: add checking for key-id in domain name format.
		#
		;;
	    C)
		#
		#-Setup the next CONTROLSSET index entry from
		# the settings in CURRENT_* after performing the following:
		#--CURRENT_CHAR="\"STANDARD\""
		#--Split up the $OPTARG into its individual parts and set
		#---CURRENT_IPADDR
		#---CURRENT_IPPORT
		#---CURRENT_ALLOW
		#---CURRENT_KEYS
		#--Set CURRENT_ID to $CURRENT_IPADDR,$CURRENT_IPPORT
		#--Increment SERVERSETindx
		#
		CURRENT_CHAR="\"\\\"STANDARD\\\"\""
##JPF##		CURRENT_ID="\"\\\"$OPTARG\\\"\""
		rndckeyadm_parseOptions_subOptions \
		 ${OPTARG//(;)/\\\1} \
		 CURRENT_IPADDR \
		 CURRENT_IPPORT \
		 CURRENT_ALLOW \
		 CURRENT_KEYS
		eval "eval" "\"if [[ (-z \"$CURRENT_IPADDR\") ]]
		then
		    #
		    #-The NULL check for ALLOW and KEYS is done when processing
		    # CONTROLSSET Array in rndckeyadm_add(). 
		    #
		    printf \\\"\noption \-C argument control-channel-list can not have empty values for either the ip_addr, address_match_list, or key_list parts\n\n\\\"
		    rndckeyadm_usage # No Return
		fi\""
		CURRENT_ID="\"\\\"$CURRENT_IPADDR,$CURRENT_IPPORT\\\"\""
		(( CONTROLSSETindx++ ))
		CONTROLSSET[$CONTROLSSETindx]="\
		 $CURRENT_CHAR \
		 $CURRENT_ID \
		 $CURRENT_IPADDR \
		 $CURRENT_IPPORT \
		 $CURRENT_ALLOW \
		 $CURRENT_KEYS \
		"
		DEBUG "IN rndckeyadm_parseOptions: -C  CONTROLSSET[$CONTROLSSETindx] == \" ${CONTROLSSET[$CONTROLSSETindx]} \""

		#
		#-OPT_C array is set to the OPTARG (in a format to be parsed
		# by rndckeyadm_parseOptions_RC) of the most recent OPT_C
		#
		eval OPT_$OPT[OPT_$OPT]=\"\$CURRENT_IPADDR \$CURRENT_IPPORT \$CURRENT_ALLOW \$CURRENT_KEYS\"

		#
		#-TODO:
		#--Add validation checks for each part of the Options Argument
		#  (i.e. the IPADDR, IPPORT, ALLOW, and KEYS part)
		#
		;;
	    D)
		;;
	    K)
		#
		#-Setup OPT_K array to process OPTRC[OPT_K]
		#
		eval OPT_$OPT[OPT_$OPT]=\"dnssec-keygen\"

		CURRENT_KEYGEN="\"\\\"dnssec-keygen\\\"\""
		;;
	    L)
		OPT="OPT_TODO"
		;;
	    S)
		#
		#-Setup OPT_S array to process OPTRC[OPT_S]
		#
		eval OPT_$OPT[OPT_$OPT]=\"\$OPTARG\"

		#
		#-Setup the DEFAULT SERVERSET index entry which is zero from
		# the settings in CURRENT_* after initializing the following:
		#--CURRENT_CHAR="\"DEFAULT\""
		#--CURRENT_NAMESERVER="\"$OPTARG\""
		#
		CURRENT_CHAR="\"\\\"DEFAULT\\\"\""
		CURRENT_NAMESERVER="\"\\\"$OPTARG\\\"\""
		SERVERSET[0]="\
		 $CURRENT_CHAR \
		 $CURRENT_NAMESERVER \
		 $CURRENT_DEFAULT_KEYID \
		"

		#
		#-TODO: add checking for server in either FQDN (without the
		# trailing doe) or IP Addr format.
		#
		;;
	    Y)
		#
		#-Setup OPT_Y array to process OPTRC[OPT_Y]
		#
		eval OPT_$OPT[OPT_$OPT]=\"\$OPTARG\"

		#
		#-Setup the DEFAULT KEYSET index entry which is zero from
		# the settings in CURRENT_* after initializing the following:
		#--CURRENT_CHAR="\"DEFAULT\""
		#--CURRENT_DEFAULT_KEYID="\"$OPTARG\""
		#
		CURRENT_CHAR="\"\\\"DEFAULT\\\"\""
		CURRENT_DEFAULT_KEYID="\"\\\"$OPTARG\\\"\""
		KEYSET[0]="\
		 $CURRENT_CHAR \
		 $CURRENT_DEFAULT_KEYID \
		 $CURRENT_KEYGEN \
		 $CURRENT_PLAINTEXT \
		 $CURRENT_KEYALGORITHM \
		"

		#
		#-TODO:
		#--add checking for key-id in domain name format.
		;;
	    \?)
		printf "invalid argument: %s\n" "$OPTCUR"
		rndckeyadm_usage # No Return
		;;
	    ':')
		printf "requires an argument: %s\n" "$OPTCUR"
		rndckeyadm_usage # No Return
		;;
	    esac

	    #
	    #-Process any Runtime Configuration Variables that have been
	    # overridden by a comand line option.
	    #
	    DEBUG "`eval \"echo\" \\\"OPT_\$OPT[OPT_\$OPT] == \\\${OPT_\$OPT[OPT_\$OPT]}\\\"`"
	    rndckeyadm_parseOptions_RC OPTRC OPT_$OPT
	done


	#
	#-An OPTIND that is less than or equal to the Number of remaining
	# paramaters to process indicates the paramater at $OPTIND may be
	# a Command Line Argument.
	#--IF $OPTIND <= $#
	#	-Save the potential Command Line Arguemnt.
	#	 Command Line Arguments will be processed after all the
	#	 Command Line Paramaters have been processed.
	#--FI
	#
	if [[ $OPTIND -le $# ]]
	then
	    eval CMDARGS[$CMDARGSindx]=\"\$$OPTIND\"
	    (( CMDARGSindx++ ))
	    shift $OPTIND
	else
	    shift $#
	fi
	DEBUG "OPTIND == $OPTIND  REMAINING NUM PARAMS == $# "
	OPTIND=1

	#
	#-break if no more options/arguments on the command line 
	#
	if [[ ${#*} -eq 0 ]]
	then
	    break
	fi
    done

    #   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #
    #
    #-All of the command line options/arguments have been individually
    # processed.  Now need to perform various criteria checks and process
    # all the options/arguments together.
    #

    #--Verify that one of the anchor options have been specified.
    #	-NOTE: If no options or command line arguments have been specified,
    #	 the default anchor option is OPT_L (i.e. -L).
    #
    if [[ (-z $OPTDETECT) && (-z $CMDARGS) ]]
    then
	OPTDETECT=OPT_L
	OPTMUTEX=OPT_L
    fi
    if [[ -z $OPTMUTEX ]]
    then
	printf \
	 "%s :\none of the required options ( %s ) has not been selected\n" \
	 ${0##*/} "$OPTREQUIRED"
	rndckeyadm_usage	# No Return
    fi

    #--For a given anchor option
    #	-Verify that the options and option and command arguments are valid for
    #	 the given anchor option.
    #	-Perform mutual exclusion option checks.
    #
    for opt in $OPTDETECT
    do
	if [[ "${OPTVALID[$OPTMUTEX]}" != ?(*$opt*) ]]
	then
	    eval "printf" \\\
	     \"%s is not valid for option %s\\\n\" \${$opt[ID]} \${$OPTMUTEX[ID]}
	    rndckeyadm_usage	# No Return
	fi
	if [[ "${OPTVALID[$OPTMUTEX]}" = ?(*$opt:*) ]]
	then
	    mutex=${OPTVALID[$OPTMUTEX]//?(*$opt:)}
	    mutex=${mutex/%?([ 	]*)}
	    if [[ "$OPTDETECT" = ?(*$mutex*) ]]
	    then
		eval "printf" \\\
		 \"%s  and  %s  are mutually exclusive\\\n\" \\\
		 \${$opt[ID]} \${$mutex[ID]}
		rndckeyadm_usage	# No Return
	    fi
	fi
    done
    case $OPTMUTEX in
    OPT_a)
	if [[ ${#CMDARGS[*]} -ne 0 ]]
	then
	    printf "%s does not support arguments \"" ${OPT_a[ID]}
	    for i in ${!CMDARGS[*]}
	    do
		printf " %s" ${CMDARGS[$i]}
	    done
	    printf " \"\n"
	    rndckeyadm_usage	# No Return
	fi
    ;; 
    OPT_d)
	# TODO
    ;; 
    OPT_D)
	# TODO
    ;; 
    OPT_L)
	# TODO
    ;; 
    esac


    #   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #
    #
    #-All option criteria checks has sucessfully passed.
    #-Perfrom all special case option processing
    #

    #
    #-SPECIAL CASE: -s AND ! -S
    #--Set the Default SERVERSET Entry's Characteristic to IGNORE
    #
    if [[ (-n ${OPT_s[OPT_s]} ) && !(-n ${OPT_S[OPT_S]}) ]]
    then
	DEBUG "SPECIAL CASE BEGIN: -s AND !(-S)"
	DEBUG "SERVERSET[0] == \"${SERVERSET[0]}\""
	set ${SERVERSET[0]}
	DEBUG "NUMBER OF SERVERSET[0] PARAMETERS == ${#*}"
	W_CHAR="$1"
	W_NAMESERVER="$2"
	W_KEYID="$3"

	DEBUG "W_CHAR == $1"
	DEBUG "W_NAMESERVER == $2"
	DEBUG "W_KEYID == $3"

	W_CHAR="\"IGNORE\""

	SERVERSET[0]="\
	 $W_CHAR \
	 $W_NAMESERVER \
	 $W_KEYID \
	"

	DEBUG "SERVERSET[0] == \"${SERVERSET[0]}\""
	DEBUG "SPECIAL CASE END: -s AND !(-S)"
    fi

    #
    #-SPECIAL CASE: -C
    #--Set the Default CONTROLSSET Entry's Characteristic to IGNORE
    #
    if [[ (-n ${OPT_C[OPT_C]} ) || (${#CONTROLSSET[*]} -gt 1) ]]
    then
	DEBUG "SPECIAL CASE BEGIN: -C"
	DEBUG "CONTROLSSET[0] == \"${CONTROLSSET[0]}\""
	set ${CONTROLSSET[0]}
	DEBUG "NUMBER OF CONTROLSSET[0] PARAMETERS == ${#*}"
	W_CHAR="$1"
	W_ID="$2"
	W_IPADDR="$3"
	W_IPPORT="$4"
	W_ALLOW="$5"
	W_KEYS="$6"

	DEBUG "W_CHAR == $1"
	DEBUG "W_ID == $2"
	DEBUG "W_IPADDR == $3"
	DEBUG "W_IPPORT == $4"
	DEBUG "W_ALLOW == $5"
	DEBUG "W_KEYS == $6"

	W_CHAR="\"IGNORE\""

	CONTROLSSET[0]="\
	 $W_CHAR \
	 $W_ID \
	 $W_IPADDR \
	 $W_IPPORT \
	 $W_ALLOW \
	 $W_KEYS \
	"

	DEBUG "CONTROLSSET[0] == \"${CONTROLSSET[0]}\""
	DEBUG "SPECIAL CASE END:  -C"
    fi


    unset OPTCUR
    unset CURRENT_NAMESERVER
    unset CURRENT_KEYID
    unset CURRENT_KEYGEN
    unset CURRENT_PLAINTEXT
    unset CURRENT_KEYALGORITHM
    unset CURRENT_ID
    unset CURRENT_IPADDR
    unset CURRENT_IPPORT
    unset CURRENT_ALLOW
    unset CURRENT_KEYS
    unset KEYSETindx
    unset SERVERSETindx
    unset CONTROLSSETindx

    DEBUG "IN rndckeyadm_parseOptions: RETURN" 
}

#
#-Sub functions for ParseOptions to handle processing that needs to use the,
# set, shell built-in.
#--This is necessary so that the originally positional paramaters assigned
#  for the command line paramaters are not compromised.
#


#
#-Process any Runtime Configuration Variables that have been overridden by
# a command line options.
#
#-The following arguments are passed in by name positionaly and typeset to
# be referenced:
#
#--$1	==	OPTRC
#--$2	==	OPT_$OPT as used as an array
#
rndckeyadm_parseOptions_RC () {

    typeset -i INDX
    typeset -n myOPTRC=$1
    typeset -n myOPT=$2
    myOPTindx=$2

	    if [[ -n ${myOPTRC[$myOPTindx]} ]]
	    then
		(( INDX=1 ))
		set -f
	 	#
		#-NOTE:
		#--The arry element's value is, set, after it has expanded
		#  its array element in order to handle case where an array
		#  element contains a qouted string as part/all of its value.
		#  The eval will allow qouted string to be set as one
		#  positional parameter (as opposed to more than one if the
		#  quoted strings contains any IFS characters).
		#
		eval "eval" \"set\" \${myOPT[$myOPTindx]}
		DEBUG "Num Positional Params == $#"
		DEBUG "Positional Params == $@"
		for RC in ${myOPTRC[$myOPTindx]}
		do
		    eval myOPT[$RC]="\$$INDX"
		    DEBUG "`eval \"echo\" \"myOPT[\$RC] == \\\${myOPT[\$RC]}\"`"
		    (( INDX+=1 ))
		done
		set +f
	    fi

    unset -n myOPTRC
    unset -n myOPT
    unset myOPTindx
}

#
#-Perform suboption parsing (i.e. a given argument's parts are separated by
# commas) and assign each suboption to one of variables passed in (by name) as
# arguments to this function.
#
#-The first argument, $1, is a string of suboptions separated by commas.
#
#-Each argument after the first argument (i.e. $2 ...) is the name of the
# variable to assign the value of the suboption.
#--NOTE:
#    -If there are more name arguements than suboptions, the remaining name
#     arguments do not have values assinged
#    -If there are less name arguments than suboptions, the remaining
#     suboptions's values are not assigned.
#

rndckeyadm_parseOptions_subOptions () {
    DEBUG "IN rndckeyadm_parseOptions_subOptions: ENTER"
    typeset -i indx
    set -f

    subOpt="$1"
    shift 1
    (( indx=0 ))
    set -A subOptNameArray
    for n in $*
    do
	subOptNameArray[$indx]="$n"
	(( indx++ ))
    done
    DEBUG "subOpt == $subOpt"
    for i in ${!subOptNameArray[*]}
    do
	DEBUG "subOptNameArray[$i] == ${subOptNameArray[$i]}"
	typeset -n subOptRef=${subOptNameArray[$i]}
	subOptRef="\"\\\"\\\"\""
	DEBUG "INITIALIZE: ${!subOptRef} == \"$subOptRef\""
	unset -n subOptRef
    done

    OIFS="$IFS"
    IFS=","
    set $subOpt
    IFS="$OIFS"
    #
    #-NOTE: command, set, does not set a positional parameter if the last entry
    # is NULL.
    #
    DEBUG "Number of subOptions Variables == ${#subOptNameArray[*]}"
    DEBUG "Number of subOptions Items == $#"
    (( indx=0 ))
    for subOptItem in "$@"
    do
	DEBUG "subOptItem == \"$subOptItem\""
	#
	#-If there are no more variable names to assign to we are done
	#
	if [[ $indx -ge ${#subOptNameArray[*]} ]]
	then
	    break
	fi

	typeset -n subOptRef=${subOptNameArray[$indx]}
	subOptRef="\"\\\"$subOptItem\\\"\""
	DEBUG "${!subOptRef} == \"$subOptRef\""
	unset -n subOptRef
	(( indx++ ))
    done
    set +f
    unset indx
    DEBUG "IN rndckeyadm_parseOptions_subOptions: RETURN"
}


rndckeyadm_usage () {

    printf "\
rndc-keyadm	-a [-c runtime-config-file] [-k plaintext-key | -K]  \\
		[-y keyid ...] [-s server ...] [-Y keyid] [-S server]
rndc-keyadm	-a [-c runtime-config-file] \\
		[-C control-channel-list ...]
rndc-keyadm	-d [-c runtime-config-file] \\
		[all-servers | -s server ...]
rndc-keyadm	-d [-c runtime-config-file] \\
		[all-controls | -C ip_addr ...]
rndc-keyadm	-D [-c runtime-config-file] [-k plaintext-key | -K] \\
		[-Y keyid] [-S server] [AVA-list] \\
		[-C control-channel-list]
rndc-keyadm	-L [-c runtime-config-file]
rndc-keyadm	[-L]
"
    exit 2
}



#   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #
#
#-rndckeyadm_setRC()
#
#-Set definitions of the Runtime Configuration Variables as defined in
# rndc-keyadm.rc(4tcp).
#-The Variables definitions can be obtained from the following places:
#    -By default within this function.
#    -From a configuration file that has either been specfied
#    --in the RNDCKEYADM_RC__FILE environment variable
#    --via the -c RNDCKEYADM_RC__FILE command line option.
#-IF the Runtime Configuration Variables are defined in a file
#    -Set the definition of the rndc-keyadm(1Mtcp) Runtime Configuration File,
#     RNDCKEYADM_RC__FILE.
#    -Dot-in the Runtime Configuration File $RNDCKEYADM_RC__FILE which activates
#     the defines of the rndc-keyadm.rc(4tcp) type variables. 
#-FI
#


rndckeyadm_setRC () {

    : ${RNDCKEYADM_RC__FILE:=}
    RNDCKEYADM_RC__FILE=${OPT_c[RNDCKEYADM_RC__FILE]:-$RNDCKEYADM_RC__FILE}

    if [[ -n $RNDCKEYADM_RC__FILE ]]
    then
	. $RNDCKEYADM_RC__FILE
	return
    fi

    #
    #-The default rndc-keyadm.rc(4tcp) variable definitions
    # for rndc-keyadm(1Mtcp)
    #

    #
    #CONVENTIONS:
    #
    #-$VARIABLE
    #	-References of this type indicate a shell-like substitutable value.
    #	 Values for given VARIABLEs are defined in the DEFINITIONS section.
    #
    #
    #
    #
    #VARIABLE DEFINITIONS:
    #
    #-Many of the following variable's values can be overridden by certain
    # rndc-keyadm(1Mtcp) command line options.
    #--The set of options a particular variable can be overridden by is
    #  listed in the particular variable's description.
    #-Each of the following variables has an effect on certain
    # rndc.conf(4tcp) and/or named.conf(4tcp) statements and substatements.
    #--The set of statements and substatements affected by a particular
    #  variable is listed in the particular variable's description.
    #
    #
    ############################################################################
    #
    #
    #--RNDCKEYADM_DNS_BIND_KEY_REPOSITORY__DIR
    #	-Default Value:
    #	--/etc/named.d/.security.d (OSR6)
    #	--/etc/inet/named.d/.security.d (UW7)
    sysname=`uname`
    if [[ "$sysname" = "SCO_SV" ]]
    then
        : ${RNDCKEYADM_DNS_BIND_KEY_REPOSITORY__DIR:=/etc/named.d/.security.d}
    else
        : ${RNDCKEYADM_DNS_BIND_KEY_REPOSITORY__DIR:=/etc/inet/named.d/.security.d}
    fi
    
    
    #--RNDCKEYADM_DNS_BIND_KEY_INCLUDE__FILE
    #	-Default Value:
    #	--/etc/named.d/.security.d/key.include (OSR6)
    #	--/etc/inet/named.d/.security.d/key.include (UW7)
    : ${RNDCKEYADM_DNS_BIND_KEY_INCLUDE__FILE:=${RNDCKEYADM_DNS_BIND_KEY_REPOSITORY__DIR}/key.include}
    
    
    
    
    #
    ############################################################################
    #
    #--RNDCKEYADM_RNDC_KEY_REPOSITORY__DIR
    #	-Default Value:
    #	--/etc/named.d/.security.d/RNDC.d (OSR6)
    #	--/etc/inet/named.d/.security.d/RNDC.d (UW7)
    : ${RNDCKEYADM_RNDC_KEY_REPOSITORY__DIR:=${RNDCKEYADM_DNS_BIND_KEY_REPOSITORY__DIR}/RNDC.d}
    
    
    #
    #--RNDCKEYADM_RNDC_INCLUDE__FILE:
    #	-Description:
    #	--contains named.conf(4tcp) style statements derived from each defined
    #	  ${RNDCKEYADM_RNDC_KEY_REPOSITORY_DIR}/NAMESERVER.D/NAMESERVER_INFO_FILE.
    #	  and derived from ${RNDCKEYADM_RNDC_INFO__FILE}.
    #	---NAMESERVER_INFO_FILEs contain the rndc(1Mtcp) related
    #	   named.conf(4tcp) style statements (i.e. key)
    #	---RNDCKEYADM_RNDC_INFO_FILE contains the rndc(1Mtcp) related
    #	   named.conf(4tcp) style statements (i.e. control inet substatements)
    #	-Default Value:
    #	--/etc/named.d/.security.d/RNDC.d/rndc.include (OSR6)
    #	--/etc/inet/named.d/.security.d/RNDC.d/rndc.include (UW7)
    : ${RNDCKEYADM_RNDC_INCLUDE__FILE:=${RNDCKEYADM_RNDC_KEY_REPOSITORY__DIR}/rndc.include}
    
    
    #--RNDCKEYADM_RNDC_INFO__FILE:
    #	-Description:
    #	--contains infomation used by rndc-keyadm(1Mtcp) when generating
    #	  ${RNDCKEYADM_RNDC_INCLUDE__FILE}
    #	-Default Value:
    #	--/etc/named.d/.security.d/RNDC.d/rndc.info (OSR6)
    #	--/etc/inet/named.d/.security.d/RNDC.d/rndc.info (UW7)
    : ${RNDCKEYADM_RNDC_INFO__FILE:=${RNDCKEYADM_RNDC_KEY_REPOSITORY__DIR}/rndc.info}
    
    
    #--RNDCKEYADM_RNDC_CONFIG__FILE:
    #	-Description:
    #	--contains rndc.conf(4tcp) style statements derived from each defined
    #	  ${RNDCKEYADM_RNDC_KEY_REPOSITORY_DIR}/NAMESERVER.D/NAMESERVER_INFO_FILE
    #	-Default Value:
    #	--/etc/named.d/.security.d/RNDC.d/rndc.conf (OSR6)
    #	--/etc/inet/named.d/.security.d/RNDC.d/rndc.conf (UW7)
    : ${RNDCKEYADM_RNDC_CONFIG__FILE:=${RNDCKEYADM_RNDC_KEY_REPOSITORY__DIR}/rndc.conf}
    
    
    #--RNDCKEYADM_RNDC_CONFIG_ROOT__FILE:
    #	-Description:
    #	--file is linked to $RNDCKEYADM_RNDC_CONFIG__FILE and is the file the
    #	  rndc(1Mtcp) utility uses to obtain its key information.
    #	-Default Value:
    #	--/etc/rndc.conf (OSR6)
    #	--/etc/inet/rndc.conf (UW7)
    if [[ "$sysname" = "SCO_SV" ]]
    then
        : ${RNDCKEYADM_RNDC_CONFIG_ROOT__FILE:=/etc/rndc.conf}
    else
        : ${RNDCKEYADM_RNDC_CONFIG_ROOT__FILE:=/etc/inet/rndc.conf}
    fi
    
    
    
    #
    ############################################################################
    #
    #--RNDCKEYADM_OPTIONS_DEFAULT_SERVER
    #	-Default Value:
    #	-- localhost
    #	-rndc-keyadm(1Mtcp) Command Line Option Override:
    #	-- -S
    #	-Effects rndc.conf(4tcp) statements/substatements:
    #	-- options default-server
    #	-Effects named.conf(4tcp) statements/substatements:
    #	-- NONE
    : ${RNDCKEYADM_OPTIONS_DEFAULT_SERVER:=localhost}
    
    
    #--RNDCKEYADM_OPTIONS_DEFAULT_KEYID
    #	-Default Value:
    #	-- localhost-rndc-key
    #	-rndc-keyadm(1Mtcp) Command Line Option Override:
    #	-- -Y
    #	-Effects rndc.conf(4tcp) statements/substatements:
    #	-- options default-key
    #	-Effects named.conf(4tcp) statements/substatements:
    #	-- NONE
    : ${RNDCKEYADM_OPTIONS_DEFAULT_KEYID:=localhost-rndc-key}
    
    
    #--RNDCKEYADM_DEFAULT_KEYALGORITHM
    #	-Default Value:
    #	-- hmac-md5
    #	-rndc-keyadm(1Mtcp) Command Line Option Override:
    #	-- NONE
    #	-Effects rndc.conf(4tcp) statements/substatements:
    #	-- key algorithm
    #	-Effects named.conf(4tcp) statements/substatements:
    #	-- key algorithm
    : ${RNDCKEYADM_DEFAULT_KEYALGORITHM:=hmac-md5}
    
    
    #--RNDCKEYADM_DEFAULT_KEYGEN
    #	-Definable Value Set
    #	-- mimencode
    #	-- dnssec-keygen
    #	-Default Value:
    #	-- dnssec-keygen
    #	-rndc-keyadm(1Mtcp) Command Line Option Override:
    #	-- -k or -K
    #	-Effects rndc.conf(4tcp) statements/substatements:
    #	-- key secret
    #	-Effects named.conf(4tcp) statements/substatements:
    #	-- key secret
    : ${RNDCKEYADM_DEFAULT_KEYGEN:=dnssec-keygen}
    
    
    #--RNDCKEYADM_DEFAULT_PLAINTEXT
    #	-Defintion:
    #	-- The default plaintext key used when security key (i.e. the
    #	   key's  secret) is generated via mimencode(1)
    #	--NOTE:
    #		-The default value should be changed since its value will/can
    #		 become well known.
    #	-Default Value:
    #	-- "Change this plaintext key string before using it on your system"
    #	-rndc-keyadm(1Mtcp) Command Line Option Override:
    #	-- -k
    #	-Effects rndc.conf(4tcp) statements/substatements:
    #	-- key secret
    #	-Effects named.conf(4tcp) statements/substatements:
    #	-- key secret
    : ${RNDCKEYADM_DEFAULT_PLAINTEXT:=Change this plaintext key string before using it on your system}
    
    
    
    
    #--RNDCKEYADM_CONTROLS_DEFAULT_IPADDR
    #	-Default Value:
    #	-- *
    : ${RNDCKEYADM_CONTROLS_DEFAULT_IPADDR:=*}
    
    
    #--RNDCKEYADM_CONTROLS_DEFAULT_IPPORT
    #	-Default Value: Set to NULL
    : ${RNDCKEYADM_CONTROLS_DEFAULT_IPPORT:=}
    
    
    #--RNDCKEYADM_CONTROLS_DEFAULT_ALLOW
    #	-Default Value:
    #	-- localhost
    : ${RNDCKEYADM_CONTROLS_DEFAULT_ALLOW:=localhost}
    

    #--RNDCKEYADM_CONTROLS_DEFAULT_KEYS
    #	-Default Value:
    #	-- localhost-rndc-key
    #    -rndc-keyadm(1Mtcp) Command Line Option Override:
    #    -- -C
    #    -Effects rndc.conf(4tcp) statements/substatements:
    #    -- NONE
    #    -Effects named.conf(4tcp) statements/substatements:
    #    -- controls inet
    : ${RNDCKEYADM_CONTROLS_DEFAULT_KEYS:=localhost-rndc-key}
}


#   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #
#-rndckeyadm_subVars()
#
#-Apply any variable substitutions to the SERVERSET, KEYSET, and CONTROLSSET
# entries that were set up when processing the command line options.
# (e.g.
#	-references to rndc-keyadm.rc(4tcp) variables may have been setup
#	 in SERVERSET, KEYSET, and CONTROLSSET entries when command line options
#	 were being processed but their values could not be accessed until
#	 after the variables definitions were activated in
#	 rndckeyadm_setRC().
# )
#
#-Redefine any rndc-keyadm.rc(4tcp) type variables values that were specified
# by a command line option.
#
#-NOTE:
#--The rndc-keyadm.rc(4tcp) variable redefines occur after the
#   SERVERSET, KEYSET, and CONTROLSSET have applied any rndc-keyadm.rc(4tcp)
#   variable.
#	-This is done so that the references to the rndc-keyadm.rc(4tcp)
#	 variables in SERVERSET, KEYSET, and CONTROLSSET are to their, as of yet
#	 unreferenceable, value before the variable's value may have been
#	 redefined.
#	 
rndckeyadm_subVars () {
    DEBUG "IN rndckeyadm_subVars: ENTER"
    set -f

    for i in ${!SERVERSET[*]}
    do
	DEBUG "IN rndckeyadm_subVars: SERVERSET[$i] == \"${SERVERSET[$i]}\""
	eval "eval"  \"set\" \"${SERVERSET[$i]}\"
	DEBUG "NUMBER OF SERVERSET[$i] PARAMETERS == ${#*}"
	W_CHAR="$1"
	W_NAMESERVER="$2"
	W_KEYID="$3"

	DEBUG "W_CHAR == $1"
	DEBUG "W_NAMESERVER == $2"
	DEBUG "W_KEYID == $3"

    done

    for i in ${!KEYSET[*]}
    do
	DEBUG "IN rndckeyadm_subVars: KEYSET[$i] == \"${KEYSET[$i]}\""
	eval "eval"  \"set\" \"${KEYSET[$i]}\"
	DEBUG "NUMBER OF KEYSET[$i] PARAMETERS == ${#*}"
	W_CHAR="$1"
	W_KEYID="$2"
	W_KEYGEN="$3"
	W_PLAINTEXT="$4"
	W_KEYALGORITHM="$5"

	DEBUG "W_CHAR == $1"
	DEBUG "W_KEYID == $2"
	DEBUG "W_KEYGEN == $3"
	DEBUG "W_PLAINTEXT == $4"
	DEBUG "W_KEYALGORITHM == $5"

    done

    for i in ${!CONTROLSSET[*]}
    do
	DEBUG "IN rndckeyadm_subVars: CONTROLSSET[$i] == \"${CONTROLSSET[$i]}\""
	eval "eval"  \"set\" \"${CONTROLSSET[$i]}\"
	DEBUG "NUMBER OF CONTROLSSET[$i] PARAMETERS == ${#*}"
	W_CHAR="$1"
	W_ID="$2"
	W_IPADDR="$3"
	W_IPPORT="$4"
	W_ALLOW="$5"
	W_KEYS="$6"

	DEBUG "W_CHAR == $1"
	DEBUG "W_ID == $2"
	DEBUG "W_IPADDR == $3"
	DEBUG "W_IPPORT == $4"
	DEBUG "W_ALLOW == $5"
	DEBUG "W_KEYS == $6"

    done

    for opt in ${!OPTRC[*]}
    do
	tmprc="${OPTRC[$opt]}"
	if [[ -n $tmprc ]]
	then
	    for rc in $tmprc
	    do
		eval tmprcv=\"\${$opt[$rc]}\"
		if [[ -n $tmprcv ]]
		then
		    DEBUG "rc == $rc   tmprcv == $tmprcv"
		    eval ${rc}=\"$tmprcv\"
		fi
	    done
	fi
    done
    set +f
    DEBUG "IN rndckeyadm_subVars: RETURN"

}

#   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #
#
#			UTILITY FUNCTIONS
#
#   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #

#   #   #   #   #
#
#-rndckeyadm_genArrayName()
#
#--Generates a child variable name as an array given the parent name and a name,
#  (typically a NameServer in either FQDN, less the root dot, or IP address
#  format, or a Key ID in domain name format), to generate the child part
#  of the array variable.
#--Assigns a reference variable name to the generated child variable.
#--NOTE:
#	-names that contain dots have the dots replaced with underscores.
#	-names that begin with a numeric value have an underscore prepended.
#	-names that contain dashes have the dashes replaced with underscores.
#    -this is necessary since variable names cannot start with a number or
#     contain a dash, and a variable with a dot must have its parent
#     (the string before the last dot) defined before defining the child
#      variable name.
#
#--Arguments
#  $1	==	Parent Part
#  $2	==	Child Name Part
#  $3	==	Reference Variable Name
#
rndckeyadm_genArrayName () {
    ArrayName=$(
        X=${2//./_}
        X=${X//[-]/_}
        X=${X/#([0-9])/_\1}
        echo $X
    )
    typeset -A ${1}.$ArrayName
    typeset -n ${3}=${1}.$ArrayName
}


#   #   #   #   #
#
# rndckeyadm_isValidIPAddr()
#
#DESCRIPTION:
#
#-Determines whether or not the argument is in a valid IPv4 or IPv6 format
#
#
#FUNCTION ARGUMENTS:
#
#-$1	==	ipaddr string
#
#
#RETURN VALUES
#
# 0	- If ipaddr argument is NOT in a valid IPv4 or IPv6 format
#
# 1	- If ipaddr argument is in a valid IPv4 or IPv6 format
#
#
#			IPv4 Address Format Rules
#			=========================
#RULE 1:
#-Minimum of 1 and Maximum of 4 distinct numbers each of which are decimal in
# base and whose value ranges from 0-255 inclusive.
#--NOTE: the numbers are represented as strings of ASCII digits 0-9.
#	(eg. 127 0 10 1)
#RULE 2:
#-Each adjacent pair of numbers is separated by the ASCII PERIOD Character.
#	(eg. Numbers 127 0 10 1  would be specified as 127.0.10.1)
#--NOTE: This impliies a Maximum of 3 ASCII PERIODS may be specified.
#RULE 3:
#-The last distinct number may optionally have the following notation appended:
#    -the ASCII SLASH Character followed by a decimal number whose value ranges
#     from 0-32.
#	(e.g. 127.0.10.1/18   127/8)
#RULE 4:
#-If the optional SLASH notation is not specified the Minimum numberof distinct
# numbers MUST be 4.
#	(e.g. 127.0.10.1)
# 
#
#
#			IPv6 Address Format Rules
#			=========================
#RULE 1:
#-Minimum of 0 and Maximum of 8 distinct numbers each of which are hexadecimal
# in base and whose value ranges from 0-ffff inclusive.
#--NOTE: the numbers are represented as strings of ASCII hex digits 0-9a-fA-F.
#	(eg. dead beef 0 9999 1234)
#RULE 2:
#-There can at most 1 of the following optional notation; 2 juxtaposed
# ASCII SEMI-COLONS (i.e.  ::  )
#--NOTE: The, :: , notatation may preceed all or follow all distinct numbers.
#RULE 3:
#-Each adjacent pair of numbers is separated by the ASCII SEMI-COLON Character.
#	(eg. Numbers dead beef 0 9999 1234 would be specified as
#	dead:beef:0:9999:1234 )
#--NOTE: One adjacent pair of distinct numbers may be separated by
#  the, :: , notation instead of the single COLON separator.
#--NOTE: This impliies a Maximum of 7 ASCII SEMI-COLONES may be specified.
#RULE 4:
#-The last distinct number OR the, :: , notation may optionally
# have the following notation appended:
#    -the ASCII SLASH Character followed by a decimal number whose value ranges
#     from 0-128.
#	(eg. dead:beef:0:9999:1234/32  abcd::/16  ::/64)
#RULE 5:
#-If the optional, :: , notation is not specified the Minimum numberof distinct
# numbers MUST be 8.
#	(eg. dead:beef:0:9999:1234:0:0:0 )
#
function rndckeyadm_isValidIPAddr {
    DEBUG ""
    if [[ -z $1 ]]
    then
	DEBUG "IN rndckeyadm_isValidIPAddr: RETURN  NULL argument"
	return 0
    fi

    typeset entry
    typeset ipaddr
    typeset separator
    typeset notValidChar
    typeset maxNumAddr
    typeset maxSlashValue
    typeset maxAddrValue

    ipaddr="$1"
    #
    #-Initialize values of variables to be used for checking IPv4 or IPv6
    # addresses.
    #--Since at least one, :, character is required in an IPv6, it will be used
    #  as the distinguish factor for initializing the variables.
    #
    if [[ $ipaddr == @(*:*) ]]
    then
	separator=":"
	notValidChar="[!0-9a-fA-F:/]"
	maxNumAddr=8
	maxSlashValue=128
	maxAddrValue=16#ffff
    else
	separator="."
	notValidChar="[!0-9./]"
	maxNumAddr=4
	maxSlashValue=32
	maxAddrValue=255
    fi

    #
    #-Check for valid set of characters 
    #
    if [[ "$ipaddr" == @(*$notValidChar*) ]]
    then
	DEBUG "IN rndckeyadm_isValidIPAddr: RETURN  ipaddr contains invalid char from  $notValidChar"
	return 0
    fi

    OIFS="$IFS"

    #
    #-For IPv6 type addresses
    #--Transpose, :: , notation to, __ , so that it is easier to distinguish it
    #  from the standard, : , separator.
    #--Check for more than one, __ , notation
    #
    #-Check for addresses ending separator since the ksh set command does not
    # correctly catch and set the NULL postitional paramaeter.
    #-Explicity Check for address of the form, ${separator}/ , which will not
    # be detected after splitting the address up based on its separator.
    #
    #-Split the address up based on its separator.
    #-Check minimum and maximum number of distinct address parts.
    #-Validate range of each address part.  
    #

    IFS="$separator"
    set $ipaddr
    IFS="$OIFS"
    if [[ $# -gt $maxNumAddr ]]
    then
	DEBUG "IN rndckeyadm_isValidIPAddr: RETURN  ipaddr has > $maxNumAddr distinct addresses, found $#"
	return 0
    fi

    if [[ $separator == ":" ]]
    then
	ipaddr=${ipaddr//::/__}
	if [[ ${ipaddr#*__} == @(*__*) ]]
	then
	    DEBUG "IN rndckeyadm_isValidIPAddr: RETURN  ipaddr contains > 1 ::  ipaddr == $ipaddr \"${ipaddr#__}\""
	    return 0
	fi
	if [[ ${ipaddr} != @(*__*) ]]
	then
	    IFS="$separator"
	    set $ipaddr
	    IFS="$OIFS"
	    if [[ $# -ne 8 ]]
	    then
		DEBUG "IN rndckeyadm_isValidIPAddr: RETURN  ipaddr does not contain exactly $maxNumAddr distinct address, found $#"
		return 0
	    fi
	fi
    fi

    if [[ ("$ipaddr" != "${ipaddr%$separator}") || \
     ("$ipaddr" != "${ipaddr%$separator/*}") ]]
    then
	DEBUG "IN rndckeyadm_isValidIPAddr: RETURN  ipaddr has either a trailing separator  $separator  or  ${separator}/"
	return 0
    fi
    IFS="$separator"
    set $ipaddr
    IFS="$OIFS"
    eval slashCheck=\${$#}
    if [[ $slashCheck == @(*/*) ]]
    then
	#
	#-Check Range of value after slash
	#
	if [[ (${slashCheck#*/} == @(*/*)) || \
	 (${slashCheck#*/} -gt $maxSlashValue) ]]
	then
	    DEBUG "IN rndckeyadm_isValidIPAddr: RETURN  ipaddr slash notation's value is > $maxSlashValue or value has another slash,  actual value == ${slashCheck#*/}"
	    return 0
	fi
	#
	#-Strip of the slash notatation
	#
	ipaddr=${ipaddr%/*}
    elif [[ $separator == "." ]]
    then
	#
	#-Number of address parts must be 4 for IPv4 if there is no Slash
	# notation
	# 
	if [[ $# -ne 4 ]]
	then
	    DEBUG "IN rndckeyadm_isValidIPAddr: RETURN  ipaddr  number of distinct addres != 4, found $#"
	    return 0
	fi
    fi
    #
    #-Remove any, :: , notatation that begins or ends the ipaddr
    #-Replace any, :: , notatation that is contained with in the ip addres with
    # a 0 in order to pass address value checks below.
    #
    if [[ $ipaddr == @(*__*) ]]
    then
	if [[ "$ipaddr" != "__" ]]
	then
	    ipaddr=${ipaddr#__}
	    ipaddr=${ipaddr%__}
	    ipaddr=${ipaddr//__/:}
	else
	    ipaddr=0
	fi
    fi

    IFS="$separator"
    set $ipaddr
    IFS="$OIFS"
    for entry in "$@"
    do
	if [[ -z $entry ]]
	then
	    DEBUG "IN rndckeyadm_isValidIPAddr: RETURN  ipaddr  part is empty"
	    return 0
	elif [[ $entry == @(*/*) ]]
	then
	    DEBUG "IN rndckeyadm_isValidIPAddr: RETURN  ipaddr  part \"$entry\" contains a slash"
	    return 0
	fi
	#
	#-Obtain base from maxAddrValue
	#
	if [[ $maxAddrValue == @(*#*) ]]
	then
	    base=${maxAddrValue%#*}#
	else
	    base=""
	fi
	if [[ (${base}${entry} -gt $maxAddrValue) ]]
	then
	    DEBUG "IN rndckeyadm_isValidIPAddr: RETURN  ipaddr  part \"$entry\" is greater than maximum address value of $maxAddrValue"
	    return 0
	fi
    done

    return 1
}

#   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #
#
#-rndckeyadm_readConfig()
#
#-Read in the current rndc security key configuration from the various .info
# files in the rndc Security Area.
#-Set the respective NSI_STATE, KI_STATE, and CI_STATE indexes for the
# NAMESERVERINFO*, KEYINFO*, and CONTROLSINFO* array variables to the
# "READ" state.
#

rndckeyadm_readConfig () {
    DEBUG "IN rndckeyadm_readConfig: ENTER"

    #
    #-Initialize variable in case $RNDCKEYADM_RNDC_INFO__FILE
    # does not exist.
    #--The define introduces the variable as the "parent" so that
    #  contructs of type parent.child can be created.
    #
    : ${CONTROLSINFO:=""}
    typeset -A CONTROLSINFO
    CONTROLSINFO[CI_NEXT]=1
    : ${NAMESERVERINFO:=""}
    typeset -A NAMESERVERINFO
    NAMESERVERINFO[NSI_DEFAULT_SERVER]=""
    : ${KEYINFO:=""}
    typeset -A KEYINFO
    set -f
    if [[ -e $RNDCKEYADM_RNDC_INFO__FILE ]]
    then
	. $RNDCKEYADM_RNDC_INFO__FILE
	for entry in ${!CONTROLSINFO.*}
	do
	    eval $entry\[CI_STATE\]\=\"READ\"
	    DEBUG "`eval \"echo\" \\\"\$entry[CI_STATE] == \\\${\$entry[CI_STATE]}\\\"`"
	done
	for entry in ${!KEYINFO.*}
	do
	    eval $entry\[KI_STATE\]\=\"READ\"
	    DEBUG "`eval \"echo\" \\\"\$entry[KI_STATE] == \\\${\$entry[KI_STATE]}\\\"`"
	done
    fi
    set +f
    CONTROLSINFO[CI_STATE]="READ"
    NAMESERVERINFO[NSI_STATE]="READ"
    NAMESERVERINFO[NSI_ORIG_DEFAULT_SERVER]="${NAMESERVERINFO[NSI_DEFAULT_SERVER]}"
    KEYINFO[KI_STATE]="READ"
    unset entry


    if [[ ! -e $RNDCKEYADM_RNDC_KEY_REPOSITORY__DIR ]]
    then
	DEBUG "IN rndckeyadm_readConfig: RETURN $RNDCKEYADM_RNDC_KEY_REPOSITORY__DIR does not exist"
	return
    fi

    for entry in ${RNDCKEYADM_RNDC_KEY_REPOSITORY__DIR}/*
    do
	if [[ -d $entry ]]
	then
	    if [[ -e ${entry}/${entry##*/}.info ]]
	    then
		. ${entry}/${entry##*/}.info
	    fi
	fi
    done
    for entry in ${!NAMESERVERINFO.*}
    do
	eval $entry\[NSI_STATE\]\=\"READ\"
	DEBUG "`eval \"echo\" \\\"\$entry[NSI_STATE] == \\\${\$entry[NSI_STATE]}\\\"`"
    done

    DEBUG "IN rndckeyadm_readConfig: RETURN"
}


#   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #
#-rndckeyadm_add()
#--rndckeyadm_addGenKey()

#
# rndckeyadm_add()
#
#-FOR each entry in CONTROLSSET that has been identified to be add/updated
# (i.e. CONTROLSSET entry's Characteristic Field is either STANDARD or DEFAULT,
# OR Not Equal to IGNORE)
#--Add/Update the following indexes to the array CONTROLSINFO.num
#  and create entries in file $RNDCKEYADM_RNDC_KEY_REPOSITORY__DIR/rndc.info
#	-CI_STATE = "WRITE"
#	-CI_ID
#	-CI_CONTROLS_INET_NAMED_SUBSTATEMENT
#	-CI_IPADDR
#	-CI_IPPORT
#	-CI_ALLOW
#	-CI_KEYS
#
#-DONE
#
#-Determine the set of nameservers from SERVERSET that need to be
# added/updated based on the current configuration of nameservers in RNDC.d
# Security Area.
#--If it is determined that a SERVERSET entry is not to be added/updated its
#  Characteristic's Field is set to IGNORE.
#--If a SERVERSET entry has the DEFAULT Characteristic set, the
#  NAMESERVERINFO.nameserver entry containing the previous DEFAULT
#  (if NAMESERVERINFO.nameserver entry is different from the SERVERSET entry)
#  will do the following:
#	-NAMESERVERINFO.nameserver's NSI_OPTIONS_RNDC_STATE index is cleared
#	 and unset
#	-NAMESERVERINFO.nameserver's NSI_STATE is set to "WRITE"
#
#-Determine the set of nameservers from KEYSET that need to be
# added/updated based on the current configuration of nameservers in RNDC.d
# Security Area.
#--If it is determined that a KEYSET entry is not to be added/updated its
#  Characteristic's Field is set to IGNORE.
#
#-Determine the set of, controls inet, substatements from CONTROLSSET that
# need to be added/updated based on the current configuration of,
# controls inet, substatements in RNDC.d Security Area.
#--If it is determined that a CONTROLSSET entry is not to be added/updated its
#  Characteristic's Field is set to IGNORE.
#
#
#-FOR each nameserver in SERVERSET that has been identified to be add/updated
# (i.e. SERVERSET entry's Characteristic Field is either STANDARD or DEFAULT,
# OR Not Equal to IGNORE)
#--Add/Update the following indexes to the array NAMESERVERINFO.nameserver
#  and create file
#  $RNDCKEYADM_RNDC_KEY_REPOSITORY__DIR/NAMESERVER.D/NAMESERVER_INFO_FILE
#	-NSI_STATE = "WRITE"
#	-NSI_SERVER_RNDC_STATEMENT
#	-NSI_NAMESERVER
#	-NSI_KEYID
#
#--Set NAMESERVERINFO[NSI_STATE]="WRITE"
#
#--IF Characteristic Field is DEFAULT add/update the following
#  NAMESERVERINFO.nameserver index
#	-NSI_OPTIONS_RNDC_STATEMENT
#
#---Set NAMESERVERINFO[NSI_DEFAULT_NAME]="${NAMESERVERINFO.nameserver[NSI_NAMESERVER]}"
#--FI
#
#-DONE
#
#-FOR each keyid in KEYSET that has been identified to be add/updated
# (i.e. KEYSET entry's Characteristic Field is either STANDARD or DEFAULT,
# OR Not Equal to IGNORE)
#--Add/Update the following indexes to the array KEYINFO.keyid
#  and create entries in file $RNDCKEYADM_RNDC_KEY_REPOSITORY__DIR/rndc.info
#	-KI_STATE = "WRITE"
#	-KI_KEY_RNDC_STATEMENT
#	-KI_KEYID
#	-KI_KEYGEN
#	-KI_PLAINTEXT	-If KI_KEYGEN == mimencode
#	-KI_KEYAGLORITHM
#	-KI_SECRET_KEY
#
#--Set KEYINFO[KI_STATE]="WRITE"
#
#--FI
#
#-DONE
#

rndckeyadm_add () {
    DEBUG "IN rndckeyadm_add: ENTER"
    set -f

    #
    #-Build a list of the current CONTROLSINFO CI_IDs to be used for comparison
    # to the W_IDs from the CONTROLSSET entries.
    #--Append the array name the CI_ID is defined in for later lookup.
    #
    CONTROLSINFO_IDS=""
    for entry in ${!CONTROLSINFO._*}
    do
	typeset -n W_CONTROLSINFO=$entry
	CONTROLSINFO_IDS="$CONTROLSINFO_IDS ${W_CONTROLSINFO[CI_ID]}:${!W_CONTROLSINFO} "
	unset -n W_CONTROLSINFO
    done
    DEBUG "CONTROLSINFO_IDS == \"$CONTROLSINFO_IDS\""

    typeset -i W_NEXT=${CONTROLSINFO[CI_NEXT]}
    for i in ${!CONTROLSSET[*]}
    do
	eval "eval"  \"set\" \"${CONTROLSSET[$i]}\"
	DEBUG "NUMBER OF CONTROLSSET[$i] PARAMETERS == ${#*}"
	W_CHAR="$1"
	W_ID="$2"
	W_IPADDR="$3"
	W_IPPORT="$4"
	W_ALLOW="$5"
	W_KEYS="$6"

	DEBUG "W_CHAR == $1"
	DEBUG "W_ID == $2"
	DEBUG "W_IPADDR == $3"
	DEBUG "W_IPPORT == $4"
	DEBUG "W_ALLOW == $5"
	DEBUG "W_KEYS == $6"

	if [[ "$W_CHAR" == "IGNORE" ]]
	then
	    DEBUG "$W_ID Characteristics set to IGNORE"
	    continue
	fi

	needNewEntry=TRUE

	if [[ "$CONTROLSINFO_IDS" == @(*$W_ID*) ]]
	then
	    #
	    #-$W_ID already exists in one of the CONTROLSINFO._* entries.
	    #-IF there are any additional entries in W_ALLOW or W_KEYS
	    # respectively for the matching CONTROLSINFO._* entry's CI_ALLOW or
	    # CI_KEYS field
	    #	-Set an indication that an additional entry was found
	    #-ELSE
	    #	-Ignore the entry.
	    #-FI
	    #
	    needNewEntry=FALSE
	    typeset -n W_CONTROLSINFO=${CONTROLSINFO_IDS//*$W_ID:(CONTROLSINFO._+([0-9]))*/\1}
	    DEBUG "Found existing ID \"$W_ID\" checking ${!W_CONTROLSINFO} for additional ALLOW and KEYS entries"
	    foundNewEntry=FALSE
	    OIFS="$IFS"
	    IFS=";"	     
	    if [[ -n $W_ALLOW ]]
	    then
		set ${W_ALLOW}
		for entry in $*
		do
		    if [[ "${W_CONTROLSINFO[CI_ALLOW]}" == @(*$entry*) ]]
		    then
			continue
		    fi
		    foundNewEntry=TRUE
		    #
		    #-Only need to find the first new entry
		    #
		    break
		done
	    fi
	    if [[ (-n $W_KEYS) && ($foundNewEntry != "TRUE") ]]
	    then
		set ${W_KEYS}
		for entry in $*
		do
		    if [[ "${W_CONTROLSINFO[CI_KEYS]}" == @(*$entry*) ]]
		    then
			continue
		    fi
		    foundNewEntry=TRUE
		    #
		    #-Only need to find the first new entry
		    #
		    break
		done
	    fi

	    IFS="$OIFS"
	    if [[ "$foundNewEntry" != "TRUE" ]]
	    then
		DEBUG "Ignoring ID == \"$W_ID\" since there is no new additional entries for W_ALLOW == \"$W_ALLOW\" or W_KEYS == \"$W_KEYS\""
		unset -n W_CONTROLSINFO
		continue
	    fi
	fi

	#
	#-Set indication that the RNDC_INFO_FILE needs to be generated.
	#--NOTE: This may be set many times.
	#
	CONTROLSINFO[CI_STATE]="WRITE"

	#
	#-Respectively Append the additional entries in W_ALLOW or
	# W_KEYS to the CONTROLSINFO._* entry's CI_ALLOW or CI_KEYS
	# field.
	#


	if [[ "$needNewEntry" == "TRUE" ]]
	then
	    typeset -A CONTROLSINFO._$W_NEXT
	    typeset -n W_CONTROLSINFO=CONTROLSINFO._$W_NEXT


	    CONTROLSINFO[CI_NEXT]=$(( W_NEXT+=1 ))

	    W_CONTROLSINFO[CI_ID]="$W_ID"
	    W_CONTROLSINFO[CI_IPADDR]="$W_IPADDR"
	    W_CONTROLSINFO[CI_IPPORT]="$W_IPPORT"
	    if [[ -n ${W_CONTROLSINFO[CI_IPPORT]} ]]
	    then
	        W_CONTROLSINFO[CI_PORT]="port ${W_CONTROLSINFO[CI_IPPORT]}"
	    fi
	fi

	CONTROLSINFO_IDS="$CONTROLSINFO_IDS ${W_CONTROLSINFO[CI_ID]}:${!W_CONTROLSINFO} "
	W_CONTROLSINFO[CI_STATE]="WRITE"

	OIFS="$IFS"
	IFS=";"
	if [[ -n $W_ALLOW ]]
	then
	    set ${W_ALLOW}
	    for entry in $*
	    do
		if [[ "${W_CONTROLSINFO[CI_ALLOW]}" == @(*$entry*) ]]
		then
		    #
		    #-Do not add duplicate new entries
		    #
		    continue
		fi
		rndckeyadm_isValidIPAddr $entry
		if [[ $? -eq 0 ]]
		then
		    #
		    #-Not a valid IPADDR.  Assume an acl_name and enclose the
		    # entry in double quoues
		    #
		    entry="\"${entry}\""
		fi
		if [[ -n ${W_CONTROLSINFO[CI_ALLOW]} ]]
		then
		    W_CONTROLSINFO[CI_ALLOW]="${W_CONTROLSINFO[CI_ALLOW]} ${entry};"
		else
		    W_CONTROLSINFO[CI_ALLOW]="${entry};"
		fi
	    done
	fi

	if [[ -n $W_KEYS ]]
	then
	    set ${W_KEYS}
	    for entry in $*
	    do
		if [[ "${W_CONTROLSINFO[CI_KEYS]}" == @(*$entry*) ]]
		then
		    #
		    #-Do not add duplicate new entries
		    #
		    continue
		fi
		#
		#-All keyid entries must be enclosed in double quotes
		#
		if [[ -n ${W_CONTROLSINFO[CI_KEYS]} ]]
		then
		    W_CONTROLSINFO[CI_KEYS]="${W_CONTROLSINFO[CI_KEYS]} \"${entry}\";"
		else
		    W_CONTROLSINFO[CI_KEYS]="\"${entry}\";"
		fi
	    done
	fi
	IFS="$OIFS"


	DEBUG "${!W_CONTROLSINFO}"
	DEBUG "CI_ID == ${W_CONTROLSINFO[CI_ID]}"
	DEBUG "CI_IPADDR == ${W_CONTROLSINFO[CI_IPADDR]}"
	DEBUG "CI_IPPORT == ${W_CONTROLSINFO[CI_IPPORT]}"
	DEBUG "CI_ALLOW == ${W_CONTROLSINFO[CI_ALLOW]}"
	DEBUG "CI_KEYS == ${W_CONTROLSINFO[CI_KEYS]}"

	#
	#-The NULL check for ALLOW and KEYS was delayed until the CONTROLSINFO
	# and CONTOLSSET entries were merged.
	#--A NULL ALLOW on the Command Line (i.e. in the CONTROLSSET entry)
	#  may not have a NULL ALLOW in an associated and previously existing
	# (i.e. matching by CI_ID) #  CONTROLSINFO._*[CI_ALLOW] 
	#
	if [[ (-z "${W_CONTROLSINFO[CI_ALLOW]}") || \
	 (-z "${W_CONTROLSINFO[CI_KEYS]}") ]]
	then
	    printf "\noption -C argument control-channel-list can not have empty values for either the ip_addr, address_match_list, or key_list parts\n\n"
	    rndckeyadm_usage # No Return
	fi


	W_CONTROLSINFO[CI_CONTROLS_INET_NAMED_SUBSTATEMENT]="\
inet ${W_CONTROLSINFO[CI_IPADDR]} \
${W_CONTROLSINFO[CI_IPPORT]} \
allow { ${W_CONTROLSINFO[CI_ALLOW]} } \
keys { ${W_CONTROLSINFO[CI_KEYS]} } ; "

	DEBUG "CI_CONTROLS_INET_NAMED_SUBSTATEMENT == \n${W_CONTROLSINFO[CI_CONTROLS_INET_NAMED_SUBSTATEMENT]}"

	unset -n W_CONTROLSINFO

    done

    #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #
    #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #

    for i in ${!SERVERSET[*]}
    do
	DEBUG "IN rndckeyadm_add: SERVERSET[$i] == \"${SERVERSET[$i]}\""
	eval "eval"  \"set\" \"${SERVERSET[$i]}\"
	DEBUG "NUMBER OF SERVERSET[$i] PARAMETERS == ${#*}"
	W_CHAR="$1"
	W_NAMESERVER="$2"
	W_KEYID="$3"

	DEBUG "W_CHAR == $1"
	DEBUG "W_NAMESERVER == $2"
	DEBUG "W_KEYID == $3"

	if [[ "$W_CHAR" = "IGNORE" ]]
	then
	    DEBUG "Ignore since no changes for $W_NAMESERVER"
	    continue
	fi

	rndckeyadm_genArrayName NAMESERVERINFO $W_NAMESERVER W_NAMESERVERINFO

	#
	#-IF the NAMESERVERINFO entry for $W_NAMESERVER exists AND the
	# NAMESERVERINFO entry for $W_NAMESERVER is Not different from the
	# SERVERSET entry for $NAMESERVER
	#    -Ignore the entry and continue
	#-ELSE (NAMESERVERINFO entry does not exists OR has changes)
	#    -Set up the NAMESERVERINFO entry for $W_NAMESERVER to be written.
	#-FI
	#
	if [[ ${#W_NAMESERVERINFO[*]} -ne 0 ]]
	then
	    if [[ ("${W_NAMESERVERINFO[NSI_KEYID]}" == "$W_KEYID" ) && \
	     ( ( (-z ${W_NAMESERVERINFO[NSI_OPTIONS_RNDC_STATEMENT]} ) && \
	     ("$W_CHAR" == "STANDARD") ) || \
	     ( (-n ${W_NAMESERVERINFO[NSI_OPTIONS_RNDC_STATEMENT]} ) && \
	     ("$W_CHAR" == "DEFAULT") ) ) \
	    ]]
	    then
		unset -n W_NAMESERVERINFO
		continue
	    fi
	fi

	#
	#-Set indication that the RNDCKEYADM_CONFIG__FILE needs to be generated.
	#--NOTE: This may be set many times.
	#
	NAMESERVERINFO[NSI_STATE]="WRITE"


	W_NAMESERVERINFO[NSI_STATE]="WRITE"
	W_NAMESERVERINFO[NSI_NAMESERVER]="$W_NAMESERVER"
	W_NAMESERVERINFO[NSI_KEYID]="$W_KEYID"

	DEBUG "${!W_NAMESERVERINFO}"
	DEBUG "NSI_KEYID == ${W_NAMESERVERINFO[NSI_KEYID]}"


	W_NAMESERVERINFO[NSI_SERVER_RNDC_STATEMENT]="\
server \"${W_NAMESERVERINFO[NSI_NAMESERVER]}\" {
	key	\"${W_NAMESERVERINFO[NSI_KEYID]}\";
};
"

	DEBUG "NSI_SERVER_RNDC_STATEMENT == \n${W_NAMESERVERINFO[NSI_SERVER_RNDC_STATEMENT]}"


	if [[ "$W_CHAR" = "DEFAULT" ]]
	then
	    W_NAMESERVERINFO[NSI_OPTIONS_RNDC_STATEMENT]="\
options {
	default-server \"${W_NAMESERVERINFO[NSI_NAMESERVER]}\";
	default-key	\"${W_NAMESERVERINFO[NSI_KEYID]}\";
};
"
	    DEBUG "NSI_OPTIONS_RNDC_STATEMENT == \n${W_NAMESERVERINFO[NSI_OPTIONS_RNDC_STATEMENT]}"
	    NAMESERVERINFO[NSI_DEFAULT_SERVER]="${W_NAMESERVERINFO[NSI_NAMESERVER]}"
	fi

	#
	#-Create NAMESERVER_INFO_FILE
	#
	#-TODO:
	#--add better error checking when making directory and file
	#
	tmpdir="$RNDCKEYADM_RNDC_KEY_REPOSITORY__DIR/${W_NAMESERVERINFO[NSI_NAMESERVER]}"
	DEBUG "tmpdir == $tmpdir"
	(umask 0077; mkdir -p $tmpdir)

##	WORKAROUND
##	WORKAROUND
##	WORKAROUND
##	WORKAROUND
##	WORKAROUND
##	WORKAROUND
	tmp=$(
	 echo ": \${NAMESERVERINFO:=}"
	 echo "typeset -A ${!W_NAMESERVERINFO}"
	 echo "${!W_NAMESERVERINFO}=("
	 for indx in ${!W_NAMESERVERINFO[*]}
	 do
	    #
	    #-Insert backslash char in front of any double quote char for
	    # output to info file.
	    #
	    ##W_NAMESERVERINFO[$indx]="${W_NAMESERVERINFO[$indx]//\"/\\\"}"
	    tmpEntry="${W_NAMESERVERINFO[$indx]//\"/\\\"}"

	    #
	    #-Generate the indexed entry
	    #
	    ##eval "eval" \\\"echo\\\" \\\"\\\\\\\t\[\$indx\]\=\\\\\\\"\\\$\{${!W_NAMESERVERINFO}\[\$indx\]\}\\\\\\\"\\\"
	    eval "echo" \"\\\t\[\$indx\]\=\\\"\$tmpEntry\\\"\"
	 done
	 echo ")"
	)
	(
	 umask 0077
	 echo "$tmp" > $tmpdir/${W_NAMESERVERINFO[NSI_NAMESERVER]}.info
	)

	unset -n W_NAMESERVERINFO

    done

    #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #
    #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #

    for i in ${!KEYSET[*]}
    do
	DEBUG "IN rndckeyadm_add: KEYSET[$i] == \"${KEYSET[$i]}\""
	eval "eval"  \"set\" \"${KEYSET[$i]}\"
	DEBUG "NUMBER OF KEYSET[$i] PARAMETERS == ${#*}"
	W_CHAR="$1"
	W_KEYID="$2"
	W_KEYGEN="$3"
	W_PLAINTEXT="$4"
	W_KEYALGORITHM="$5"

	DEBUG "W_CHAR == $1"
	DEBUG "W_KEYID == $2"
	DEBUG "W_KEYGEN == $3"
	DEBUG "W_PLAINTEXT == $4"
	DEBUG "W_KEYALGORITHM == $5"

	if [[ "$W_CHAR" = "IGNORE" ]]
	then
	    DEBUG "Ignore since no changes for $W_KEYID"
	    continue
	fi

	rndckeyadm_genArrayName KEYINFO $W_KEYID W_KEYINFO

	#
	#-IF the KEYINFO entry for $W_KEYID exists AND the
	# KEYINFO entry for $W_KEYID is Not different from the
	# KEYSET entry for $NAMESERVER
	#    -Ignore the entry and continue
	#-ELSE (KEYINFO entry does not exists OR has changes)
	#    -Set up the KEYINFO entry for $W_KEYID to be written.
	#-FI
	#
	if [[ ${#W_KEYINFO[*]} -ne 0 ]]
	then
	    if [[ ("${W_KEYINFO[KI_KEYID]}" == "$W_KEYID" ) && \
	     ("${W_KEYINFO[KI_KEYGEN]}" == "$W_KEYGEN" ) && \
	     ( ("${W_KEYINFO[KI_KEYGEN]}" == "dnssec-keygen" ) || \
	     ("${W_KEYINFO[KI_PLAINTEXT]}" == "$W_PLAINTEXT" ) ) \
	    ]]
	    then
	 	unset -n W_KEYINFO
		continue
	    fi
	fi

	#
	#-Set indication that the RNDCKEYADM_CONFIG__FILE needs to be generated.
	#--NOTE: This may be set many times.
	#
	KEYINFO[KI_STATE]="WRITE"


	W_KEYINFO[KI_STATE]="WRITE"
	W_KEYINFO[KI_KEYID]="$W_KEYID"
	W_KEYINFO[KI_KEYGEN]="$W_KEYGEN"
	W_KEYINFO[KI_PLAINTEXT]="$W_PLAINTEXT"
	W_KEYINFO[KI_KEYALGORITHM]="$W_KEYALGORITHM"
	rndckeyadm_addGenKey W_KEYINFO

	DEBUG "${!W_KEYINFO}"
	DEBUG "KI_KEYID == ${W_KEYINFO[KI_KEYID]}"
	DEBUG "KI_KEYGEN == ${W_KEYINFO[KI_KEYGEN]}"
	DEBUG "KI_PLAINTEXT == ${W_KEYINFO[KI_PLAINTEXT]}"
	DEBUG "KI_KEYALGORITHM == ${W_KEYINFO[KI_KEYALGORITHM]}"
	DEBUG "KI_SECRET_KEY == ${W_KEYINFO[KI_SECRET_KEY]}"


	W_KEYINFO[KI_KEY_RNDC_STATEMENT]="\
key \"${W_KEYINFO[KI_KEYID]}\" {
	algorithm	\"${W_KEYINFO[KI_KEYALGORITHM]}\";
	secret	\"${W_KEYINFO[KI_SECRET_KEY]}\";
};
"

	DEBUG "KI_KEY_RNDC_STATEMENT == \n${W_KEYINFO[KI_KEY_RNDC_STATEMENT]}"

	unset -n W_KEYINFO

    done

    #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #
    #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #


    #
    #-Create/Update RNDC_INFO_FILE
    #--if any new or modified, controls inet, substatements have been added.
    #--if any new or modified, key , statements have been added.
    #--if a new default server has been defined
    #
    if [[ (${CONTROLSINFO[CI_STATE]} == "WRITE") || \
     (${KEYINFO[KI_STATE]} == "WRITE") || \
     (${NAMESERVERINFO[NSI_DEFAULT_SERVER]} != \
     ${NAMESERVERINFO[NSI_ORIG_DEFAULT_SERVER]}) ]]
    then
	#
	#-TODO:
	#--add better error checking when making file
	#
	tmp=$(
	 echo ": \${NAMESERVERINFO:=}"
	 echo "typeset -A NAMESERVERINFO"
	 echo "NAMESERVERINFO=("
	 echo "[NSI_DEFAULT_SERVER]=\"${NAMESERVERINFO[NSI_DEFAULT_SERVER]}\""
	 echo ")"
	 echo ""
	 echo ": \${KEYINFO:=}"
	 echo "typeset -A KEYINFO"
	 echo ""
	 echo ": \${CONTROLSINFO:=}"
	 echo "typeset -A CONTROLSINFO"
	 echo "CONTROLSINFO=("
	 echo "[CI_NEXT]=${CONTROLSINFO[CI_NEXT]}"
	 echo ")"
	)
	(umask 0077; echo "$tmp\n" >$RNDCKEYADM_RNDC_INFO__FILE)
	for tmpvar in ${!CONTROLSINFO.*}
	do
	    typeset -n W_CONTROLSINFO=$tmpvar
	    tmp=$(
	     echo "\ntypeset -A ${!W_CONTROLSINFO}"
	     echo "${!W_CONTROLSINFO}=("
	     for indx in ${!W_CONTROLSINFO[*]}
	     do
		#
		#-Insert backslash char in front of any double quote char for
		# the info file.
		#
		##W_CONTROLSINFO[$indx]="${W_CONTROLSINFO[$indx]//\"/\\\"}"
		tmpEntry="${W_CONTROLSINFO[$indx]//\"/\\\"}"

		#
		#-Generate the indexed entry
		#
		##eval "eval" \\\"echo\\\" \\\"\\\\\\\t\[\$indx\]\=\\\\\\\"\\\$\{${!W_CONTROLSINFO}\[\$indx\]\}\\\\\\\"\\\"
		eval "echo" \"\\\t\[\$indx\]\=\\\"\$tmpEntry\\\"\"
	     done
	     echo ")"
	    )
	    echo "$tmp"
	    unset -n W_CONTROLSINFO
	done >> $RNDCKEYADM_RNDC_INFO__FILE
	for tmpvar in ${!KEYINFO.*}
	do
	    typeset -n W_KEYINFO=$tmpvar
	    tmp=$(
	     echo "\ntypeset -A ${!W_KEYINFO}"
	     echo "${!W_KEYINFO}=("
	     for indx in ${!W_KEYINFO[*]}
	     do
		#
		#-Insert backslash char in front of any double quote char for
		# the info file.
		#
		##W_KEYINFO[$indx]="${W_KEYINFO[$indx]//\"/\\\"}"
		tmpEntry="${W_KEYINFO[$indx]//\"/\\\"}"

		#
		#-Generate the indexed entry
		#
		##eval "eval" \\\"echo\\\" \\\"\\\\\\\t\[\$indx\]\=\\\\\\\"\\\$\{${!W_KEYINFO}\[\$indx\]\}\\\\\\\"\\\"
		eval "echo" \"\\\t\[\$indx\]\=\\\"\$tmpEntry\\\"\"
	     done
	     echo ")"
	    )
	    echo "$tmp"
	    unset -n W_KEYINFO
	done >> $RNDCKEYADM_RNDC_INFO__FILE
    fi

    DEBUG "IN rndckeyadm_add: RETURN"
    set +f
}


#
# rndckeyadm_addGenKey()
#
#DESCRIPTION:
#
#-Generates the secret key that is used by the rndc.conf(4tcp) and
# named.conf(4tcp), key statement.
#
#
#FUNCTION ARGUMENTS:
#
#-KEYINFO.keyid array
#	-contains the information on how the secret key is to be generated.
#
#
#SIDE EFFECTS:
#
#-The KEYINFO.keyid's KI_SECRET_KEY array index is set to the
# secret key.
#
#

rndckeyadm_addGenKey () {

    typeset -n myKEYINFO=$1

    DEBUG "my KI_KEYGEN == ${myKEYINFO[KI_KEYGEN]}"
    case ${myKEYINFO[KI_KEYGEN]} in
    mimencode)
	myKEYINFO[KI_SECRET_KEY]="`mimencode<(echo \"${myKEYINFO[KI_PLAINTEXT]}\")`"
    ;;
   dnssec-keygen)
	#
	#-TODO:
	#--Need to implement more sophisiticated checking if other symmetric
	#  encryption algorithms, besides HMAC-MD5, are supported for TSIG.
	#
        #--For now force the alogorithm to be HMAC-MD5 regardless of the
	#  the value in KEYINFO.nameserver[KI_KEYALGORITHM]. 
	#--The keysize will be between 300 and 512.
        #
	#-NOTE:
	#--The resetting of the IFS below is a work around for an unexplained
	#  interaction with the, set, command executed here and the, set,
	#  command executed by the caller.  It appears that the IFS is changed
	#  internally in the ksh causing the calling function to set positional
	#  parameters in an unexpected way.
	#--The unset of keySize is a work around for an unxeplained ksh core
	#  dump that occurs if this function is called more than once.
	#
	typeset -i keySize=0
	while (( $keySize<300 ))
	do
	    keySize=$(( `random 255`+$keySize%512 ))
	done
	keyName=`dnssec-keygen \
	 -a HMAC-MD5 \
	 -b $keySize \
	 -n USER \
	 RNDC_$$ \
	`
	unset keySize
	OIFS="$IFS"
	while read line
	do
	    set $line
	    if [[ "$1" = "Key:" ]]
	    then
		myKEYINFO[KI_SECRET_KEY]="$2"
		break
	    fi
	done < ${keyName}.private
	rm ${keyName}.private ${keyName}.key
	IFS="$OIFS"
    ;;
    esac

    unset -n myKEYINFO
}



#   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #
#
#-rndckeyadm_delete() TODO
rndckeyadm_delete () {
    printf "TODO: Delete Functionality"
}
#-rndckeyadm_define() TODO
rndckeyadm_define () {
    printf "TODO: Define Functionality"
}
#-rndckeyadm_list() TODO
rndckeyadm_list () {
    printf "TODO: List Functionality"
}



#   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #
#
#-rndckeyadm_writeConfig()
#
#-Conditionally generate
# RNDCKEYADM_RNDC_CONFIG__FILE and RNDCKEYADM_RNDC_INCLUDE__FILE
#
#-IF NAMESERVERINFO[NSI_STATE] == "WRITE" OR KEYINFO[KI_STATE] == "WRITE"
#    -generate $RNDCKEYADM_RNDC_CONFIG__FILE and link it to
#     RNDCKEYADM_RNDC_CONFIG_ROOT__FILE
#    -generate RNDCKEYADM_RNDC_INCLUDE__FILE
#-ELSE IF CONTROLSINFO[CI_STATE] == "WRITE" OR KEYINFO[KI_STATE] == "WRITE"
#    -generate RNDCKEYADM_RNDC_INCLUDE__FILE
#-FI
#

rndckeyadm_writeConfig () {

    if [[ (${NAMESERVERINFO[NSI_STATE]} == "WRITE") || \
     ( ${KEYINFO[KI_STATE]} == "WRITE") ]]
    then
	#
	#-Generate RNDCKEYADM_RNDC_CONFIG__FILE
	# 
	tmp=$(
	    #
	    #-Get, options, statement.
	    if [[ -n ${NAMESERVERINFO[NSI_DEFAULT_SERVER]} ]]
	    then     
		rndckeyadm_genArrayName NAMESERVERINFO ${NAMESERVERINFO[NSI_DEFAULT_SERVER]} W_NAMESERVERINFO
		echo "${W_NAMESERVERINFO[NSI_OPTIONS_RNDC_STATEMENT]}"
		echo ""
		unset -n W_NAMESERVERINFO
	    fi
	    for tmpNameserver in ${!NAMESERVERINFO.*}
	    do
		typeset -n W_NAMESERVERINFO=$tmpNameserver
		echo "${W_NAMESERVERINFO[NSI_SERVER_RNDC_STATEMENT]}"
		echo ""
		unset -n W_NAMESERVERINFO
	    done
	    echo ""
	    for tmpkey in ${!KEYINFO.*}
	    do
		typeset -n W_KEYINFO=$tmpkey
		echo "${W_KEYINFO[KI_KEY_RNDC_STATEMENT]}"
		echo ""
		unset -n W_KEYINFO
	    done
	)
	(
	 umask 0077
	 echo "$tmp" >$RNDCKEYADM_RNDC_CONFIG__FILE
	 if [[ (! $RNDCKEYADM_RNDC_CONFIG__FILE -ef \
	  $RNDCKEYADM_RNDC_CONFIG_ROOT__FILE) ]] 
	 then
	    ln $RNDCKEYADM_RNDC_CONFIG__FILE $RNDCKEYADM_RNDC_CONFIG_ROOT__FILE
	    DEBUG "Created $RNDCKEYADM_RNDC_CONFIG_ROOT__FILE"
	 fi
	)
    fi
    DEBUG "Created $RNDCKEYADM_RNDC_CONFIG__FILE"


    if [[ (${KEYINFO[KI_STATE]} = "WRITE") || \
     (${CONTROLSINFO[CI_STATE]} = "WRITE") ]]
    then
	#
	#-Generate RNDCKEYADM_RNDC_INCLUDE__FILE
	# 
	tmp=$(
	    #
	    #-Generate, controls, statement.
	    #
	    echo "controls {"
	    for tmpControls in ${!CONTROLSINFO.*}
	    do
		typeset -n W_CONTROLSINFO=$tmpControls
		echo "${W_CONTROLSINFO[CI_CONTROLS_INET_NAMED_SUBSTATEMENT]}"
		unset -n W_CONTROLSINFO
	    done
	    echo "};"
	    echo ""
	    #
	    #-Generate, key, statement(s).
	    #
	    for tmpkey in ${!KEYINFO.*}
	    do
		typeset -n W_KEYINFO=$tmpkey
		echo "${W_KEYINFO[KI_KEY_RNDC_STATEMENT]}"
		echo ""
		unset -n W_KEYINFO
	    done
	)
	(
	 umask 0077
	 echo "$tmp" >$RNDCKEYADM_RNDC_INCLUDE__FILE
	)
    fi
    DEBUG "Created $RNDCKEYADM_RNDC_INCLUDE__FILE"
}



################################################################################
#					MAIN
################################################################################
#
#-Parse and process the command line options and arguments.
#	-rndckeyadm_parseOptions()
#	--rndckeyadm_parseOptions_RC()
#	--rndckeyadm_parseOptions_subOptions()
#
#-Set definitions for the Runtime Configuration Variables specified in
# rndc-keyadm.rc(4tcp).
#	-rndckeyadm_setRC()
#
#-Apply any variable substitutions to the SERVERSET and CONTROLSSET
# entries from variable defined in the rndc-keyadm.rc(4tcp) file. 
#-Apply any variable substitutions to the variables defined in the
# rndc-keyadm.rc(4tcp) file from the selected command line options.
#	-rndckeyadm_subVars()
#
#-Read in the current rndc security key configuration from the
# rndc Security Area.
#	-rndckeyadm_readConfig()
#
#-Perform the selected Anchor Operation:
#	-a	rndckeyadm_add()
#	-d	rndckeyadm_delete()
#	-D	rndckeyadm_define()
#	-L	rndckeyadm_list()
#
#-If the rndc security configuration has changed
#--Write out the new/modified rndc security key configuration into the
#  rndc Security Area.
#	-rndckeyadm_writeConfig()
#

: ${RNDCKEYADM_DEBUG:=}
if [[ -n $RNDCKEYADM_DEBUG ]]
then
    eval "
	DEBUG () {
	    echo \$*
	}
    "
else
    eval "
	DEBUG () {
	    : \$*
	}
    "
fi


rndckeyadm_parseOptions "$@"
rndckeyadm_setRC
rndckeyadm_subVars

#
#-Make sure $RNDCKEYADM_RNDC_KEY_REPOSITORY__DIR is defined
#
if [[ ! -d $RNDCKEYADM_RNDC_KEY_REPOSITORY__DIR ]]
then
    (
     umask 0077
     mkdir -p $RNDCKEYADM_RNDC_KEY_REPOSITORY__DIR >/dev/null 2>&1
    ) 
fi
cd $RNDCKEYADM_RNDC_KEY_REPOSITORY__DIR

rndckeyadm_readConfig

case $OPTMUTEX in
OPT_a)
    rndckeyadm_add
;;
OPT_d)
    rndckeyadm_delete
;;
OPT_D)
    rndckeyadm_define
;;
OPT_L)
    rndckeyadm_list
;;
esac

rndckeyadm_writeConfig

#
#-Make sure $RNDCKEYADM_DNS_BIND_KEY_INCLUDE__FILE exists and its contents
# contain:
# include "$RNDCKEYADM_RNDC_INCLUDE__FILE" ;
#
#-TODO:
#--When dnssec-keyadm(1Mtcp) is implemented, it should generate the contents
#  of $RNDCKEYADM_DNS_BIND_KEY_INCLUDE__FILE
#
if [[ ! -e $RNDCKEYADM_DNS_BIND_KEY_INCLUDE__FILE ]]
then
    (
     umask 0077
     tmp=$(
	echo "include \"$RNDCKEYADM_RNDC_INCLUDE__FILE\" ;"
     )
     echo "$tmp" > $RNDCKEYADM_DNS_BIND_KEY_INCLUDE__FILE
    )
fi
