#
#  ABFAB Trust router policies.
#
#	$Id: ba088ac1d15c23ce675bf8349b5ad285c5a047da $
#


#
# Verify rp parameters
#
abfab_psk_authorize {
	if (&TLS-PSK-Identity) {
		# TODO: may need to check trust-router-apc as well
		if ("%psksql(select distinct keyid from authorizations_keys where keyid = '%{tls-psk-identity}' and '%{trust-router-coi}' like coi and '%{gss-acceptor-realm-name}' like acceptor_realm and '%{gss-acceptor-host-name}' like hostname;)") {
			# do things here
		}
		else {
			&reply.Reply-Message = "RP not authorized for this ABFAB request"

			reject
		}
	}
}

abfab_client_check {
	# check that the acceptor host name is correct
	if ("%client(gss_acceptor_host_name)" && &GSS-acceptor-host-name) {
		if ("%client(gss_acceptor_host_name)" != "%{gss-acceptor-host-name}") {
			&reply.Reply-Message = "GSS-Acceptor-Host-Name incorrect"

			reject
		}
	}

	# set trust-router-coi attribute from the client configuration
	if ("%client(trust_router_coi)") {
		&request.Trust-Router-COI := "%client(trust_router_coi)"
	}

	# set gss-acceptor-realm-name attribute from the client configuration
	if ("%client(gss_acceptor_realm_name)") {
		&request.GSS-Acceptor-Realm-Name := "%client(gss_acceptor_realm_name)"
	}
}

#  A policy which is used to validate channel-bindings.
#
abfab_channel_bindings {
	if (&GSS-Acceptor-Service-Name && (&outer.request.GSS-Acceptor-Service-Name != &GSS-Acceptor-Service-Name)) {
		reject
	}

	if (&GSS-Acceptor-Host-Name && &outer.request.GSS-Acceptor-Host-Name != &GSS-Acceptor-Host-Name) {
		reject
	}

	if (&GSS-Acceptor-Realm-Name && &outer.request.GSS-Acceptor-Realm-Name != &GSS-Acceptor-Realm-Name) {
		reject
	}

	if (&GSS-Acceptor-Service-Name || &GSS-Acceptor-Realm-Name || &GSS-Acceptor-Host-Name) {
		&control.Chbind-Response-Code := success

		#
		#  ACK the attributes in the request.
		#
		#  If any one of these attributes don't exist in the request,
		#  then they won't be copied to the reply.
		#
		&reply.GSS-Acceptor-Service-Name = &GSS-Acceptor-Service-Name
		&reply.GSS-Acceptor-Host-Name = &GSS-Acceptor-Host-Name
		&reply.GSS-Acceptor-Realm-Name = &GSS-Acceptor-Realm-Name
	}

	#
	#  Return "handled" so that the "authenticate" section isn't used.
	#
	handled
}
