# -*- text -*-
######################################################################
#
#	Reverse CoA over RADIUS/TLS, server side.
#
#	A RADIUS/TLS client (a NAS, or a proxy in front of it) connects
#	to us, and we send CoA-Request and Disconnect-Request packets
#	back down that same connection.  See draft-ietf-radext-reverse-coa
#	and RFC 8559, Section 3.2.
#
#	The feature is experimental.  The server has to be built with
#
#		CFLAGS += -DWITH_COA_TUNNEL
#
#	added to Make.inc after running configure.  "radiusd -xxv" then
#	lists "recv-coa-from-home-server : yes".
#
#	To use this file, copy it to sites-enabled/ and remove the
#	"default" and "tls" sites, or merge the listen sections.
#
######################################################################

#
#  Listen for RADIUS/TLS.  The "+coa" suffix is what allows reverse
#  CoA on the connections accepted here.  It requires TLS.
#
listen {
	ipaddr = *
	port = 2083
	type = auth+acct+coa
	proto = tcp
	virtual_server = reverse-coa
	clients = radsec

	limit {
		max_connections = 16

		#
		#  A reverse CoA connection has to stay open, or the
		#  route to the NAS is lost until the NAS reconnects and
		#  we send our next reply on the new connection.  Do NOT
		#  leave both values at 0: with no lifetime, an
		#  idle_timeout of 0 is turned into 30 seconds.  Set a
		#  lifetime and no idle timeout:
		#
		lifetime = 86400
		idle_timeout = 0
	}

	#
	#  Timers for the packets we send down the connection.
	#  Nothing is sent twice over a stream: "mrd" is how long we
	#  wait for the NAS to answer, and packets originated with
	#  "update coa" give up at "mrc" or "mrd", whichever is first.
	#
	#  "max_keys" bounds the number of keys one connection may
	#  carry (see post-auth and accounting below).  A key per
	#  session on a busy peer adds up; the default is 4096.
	#
	coa {
		irt = 2
		mrt = 16
		mrc = 5
		mrd = 30
		max_keys = 4096
	}

	tls {
		private_key_password = whatever
		private_key_file = ${certdir}/server.pem
		certificate_file = ${certdir}/server.pem
		ca_file = ${cadir}/ca.pem
		require_client_cert = yes
	}
}

#
#  The clients which may connect over TLS.  The same idle_timeout
#  rule applies here: the smaller of the client and the listener
#  values is used, and the client default is 30.
#
#  "reverse_coa" is the per-client flag of the draft (Section 4).
#  It defaults to "yes".  A peer which does not take CoA on its own
#  connection gets "no": no key is ever attached to its connections
#  and nothing is sent down them, even on a "+coa" listener.
#
clients radsec {
	client hub-peer {
		ipaddr = 192.0.2.0/24
		proto = tls
		secret = radsec
		reverse_coa = yes

		limit {
			idle_timeout = 0
		}
	}

	client legacy-peer {
		ipaddr = 198.51.100.0/24
		proto = tls
		secret = radsec
		reverse_coa = no
	}
}

server reverse-coa {
	authorize {
		preprocess
		eap {
			ok = return
		}
		files
		mschap
		pap
	}

	authenticate {
		Auth-Type PAP {
			pap
		}
		Auth-Type MS-CHAP {
			mschap
		}
		eap
	}

	preacct {
		preprocess
		acct_unique
	}

	#
	#  A key which names the session is attached on the
	#  Accounting-Response to the Start, and removed on the Stop.
	#  A leading '-' removes a key.  With that key, a CoA for the
	#  session goes down the connection which carried it, even when
	#  the peer has several connections to us with the same
	#  Operator-Name.
	#
	accounting {
		if (&Operator-Name && &Acct-Session-Id) {
			if (&Acct-Status-Type == Start) {
				update control {
					&Originating-Realm-Key := "sid:%{Acct-Session-Id}"
				}
			}
			elsif (&Acct-Status-Type == Stop) {
				update control {
					&Originating-Realm-Key := "-sid:%{Acct-Session-Id}"
				}
			}
		}

		detail
	}

	#
	#  Keys are attached when we send a reply on the connection for
	#  a request which has control:Originating-Realm-Key.  A
	#  connection can carry many keys; each is compared with
	#  Proxy-To-Originating-Realm as a string.  Here every
	#  connection gets the realm key from Operator-Name, and a key
	#  for the Chargeable-User-Identity we issued, when we issued
	#  one.
	#
	post-auth {
		if (&Operator-Name) {
			update control {
				&Originating-Realm-Key := "%{Operator-Name}"
			}

			if (&reply:Chargeable-User-Identity) {
				update control {
					&Originating-Realm-Key += "cui:%{reply:Chargeable-User-Identity}"
				}
			}
		}

		Post-Auth-Type REJECT {
			attr_filter.access_reject
		}
	}
}

#
#  Where the CoA / Disconnect requests come from: a policy server, or
#  a script with radclient.  The listener gets its own client, so that
#  requests from other sources on this host do not change what it
#  requires.
#
listen {
	type = coa
	ipaddr = 127.0.0.1
	port = 3799
	virtual_server = reverse-coa-originate
	clients = reverse-coa-originate
}

clients reverse-coa-originate {
	client localhost {
		ipaddr = 127.0.0.1
		secret = testing123
	}
}

server reverse-coa-originate {
	#
	#  Route by Operator-Name (RFC 8559, Section 3.2).  A request
	#  without it cannot be routed: answer with Missing-Attribute.
	#  An unknown key gets Proxy-Request-Not-Routable (502) from
	#  the server, and a known key whose connections are all full
	#  gets Proxy-Processing-Error (505).
	#
	#  The routing keys are tried in order.  The session key goes
	#  first, so that the request follows the connection which
	#  carried the session, and the realm key is the fallback for
	#  a peer which only has the one connection, or whose session
	#  key has gone.
	#
	#  A request with a stale Event-Timestamp is a replay or a
	#  clock problem (RFC 5176, Section 6.3): reject it.
	#
	recv-coa {
		if (!&Operator-Name) {
			update reply {
				&Error-Cause := Missing-Attribute
			}
			reject
		}

		if (&Event-Timestamp) {
			if (("%{expr: %l - %{integer:Event-Timestamp}}" > 300) || ("%{expr: %{integer:Event-Timestamp} - %l}" > 300)) {
				update reply {
					&Error-Cause := Invalid-Request
				}
				reject
			}
		}

		if (&Acct-Session-Id) {
			update control {
				&Proxy-To-Originating-Realm := "sid:%{Acct-Session-Id}"
			}
		}
		if (&Chargeable-User-Identity) {
			update control {
				&Proxy-To-Originating-Realm += "cui:%{Chargeable-User-Identity}"
			}
		}
		update control {
			&Proxy-To-Originating-Realm += &Operator-Name
		}
		ok
	}

	send-coa {
		ok
	}
}
