3.123. <TACACSContextSQL>

This optional clause specifies an external SQL database for storing TACACS+ authentication context information for <ServerTACACSPLUS>.
TACACSContextSQL is used with ServerTACACSPLUS when TACACS+ authorization requests may arrive at a Radiator instance that does not have the required authentication context in memory. This can happen, for example, after a server restart or when TACACS+ authentication and authorization requests are routed to different Radiator instances. The context database can be shared by multiple Radiator instances. It can also be used by a single Radiator server to share contexts between FarmSize worker processes, or in simple setups to persist TACACS+ contexts over Radiator restarts
TACACSContextSQL stores the TACACS+ context created after a successful authentication. The stored data includes the user name, peer address and port information, TACACS+ port and remote address information, creation and expiry times, and the reply attributes that are needed later for TACACS+ authorization processing. When an authorization request is received and the context is not available in memory, ServerTACACSPLUS can fetch the context from the SQL database by using the TACACSContextSQL object.
A TACACSContextSQL object is selected by configuring its Identifier and referring to that identifier from ServerTACACSPLUS with the TACACSContext parameter. Here is an example:
<TACACSContextSQL>
    Identifier TACACSCTXDB

    DBSource    dbi:mysql:tacacs;host=%{GlobalVar:dbserver};port=3306
    DBUsername  %{GlobalVar:dbusername}
    DBAuth      %{GlobalVar:dbpassword}

    PurgeInterval 600
    ContextTimeout 300
</TACACSContextSQL>

<ServerTACACSPLUS>
    TACACSContext TACACSCTXDB
    AuthorizationTimeout 100
    ...
</ServerTACACSPLUS>
With this example configuration, a successful TACACS+ authentication stores the context and reply attributes in the SQL database. A later authorization request can use the in-memory context when it is still present. If the in-memory context is not present but the database context has not expired, the context is read from the database and authorization can continue. If no valid context is found, authorization is denied.
You can configure the SQL database or databases that <TACACSContextSQL> uses by defining one or more sets of DBSource, DBUsername and DBAuth parameters. It supports the common SQL configuration parameters.
ContextTimeout in TACACSContextSQL may be different from AuthorizationTimeout in <ServerTACACSPLUS>. This allows contexts to be kept in memory for a shorter time while remaining available from the database for a longer time. When a context is read from the database, the in-memory context on that Radiator instance is currently given the full AuthorizationTimeout.
See goodies files tacacsplusserver-context.cfg for a sample configuration file and tacacsplusserver-context.sql for the SQL tables matching the default SQL queries.

3.123.1. Table

This optional parameter specifies the SQL table name used to generate default SQL statements when explicit SQL statements are not supplied. It defaults to TACACS_CONTEXT
If you use the default queries, the table must have the columns expected by those queries. If you use a different schema, define the query parameters explicitly to match your schema.
# Table for wired switch management
Table TAC_CONTEXT_WIRED

3.123.2. AddContextQuery and AddContextQueryParam

This SQL statement is used to insert a new TACACS+ context into the database. By default, <TACACSContextSQL> first runs DeleteContextQuery for the same context_key and then runs AddContextQuery. If ReplaceContextQuery is defined, ReplaceContextQuery is used instead of the delete and add sequence. Special formatting characters are supported.
In the default query:
  • %0 is replaced by the SQL quoted context_key.
  • %1 is replaced by the SQL quoted user name.
  • %2 is replaced by the SQL quoted peer IP address.
  • %3 is replaced by the peer port.
  • %4 is replaced by the SQL quoted TACACS+ port.
  • %5 is replaced by the SQL quoted TACACS+ remote address.
  • %6 is replaced by the context creation time as a Unix timestamp.
  • %7 is replaced by the context expiry time as a Unix timestamp.
  • %8 is replaced by the SQL quoted serialized reply attributes.
The default query is:
INSERT INTO TACACS_CONTEXT
(context_key,username,peer_ip,peer_port,tacacs_port,
 tacacs_rem_addr,created_at,expires_at,rp_attrs)
VALUES (%0,%1,%2,%3,%4,%5,%6,%7,%8)
When one or more AddContextQueryParam parameters are defined, AddContextQuery is used with SQL bind variables instead. Special formatting characters in AddContextQueryParam are replaced before being bound to the SQL statement.
For AddContextQueryParam:
  • %0 is the unquoted context_key.
  • %1 is the unquoted user name.
  • %2 is the unquoted peer IP address.
  • %3 is the peer port.
  • %4 is the unquoted TACACS+ port.
  • %5 is the unquoted TACACS+ remote address.
  • %6 is the context creation time as a Unix timestamp.
  • %7 is the context expiry time as a Unix timestamp.
  • %8 is the unquoted serialized reply attributes.
For example:
AddContextQuery INSERT INTO TACACS_CONTEXT \
                (context_key,username,peer_ip,peer_port,tacacs_port, \
                 tacacs_rem_addr,created_at,expires_at,rp_attrs) \
                VALUES (?,?,?,?,?,?,?,?,?)
AddContextQueryParam %0
AddContextQueryParam %1
AddContextQueryParam %2
AddContextQueryParam %3
AddContextQueryParam %4
AddContextQueryParam %5
AddContextQueryParam %6
AddContextQueryParam %7
AddContextQueryParam %8

3.123.3. GetContextQuery and GetContextQueryParam

This SQL query is used to fetch a TACACS+ context from the database. It is executed when ServerTACACSPLUS needs context information that is not available in memory.
A GetContextQuery, that matches the default AddContextQuery, must return these columns, in this order:
  • username
  • peer_ip
  • peer_port
  • tacacs_port
  • tacacs_rem_addr
  • created_at
  • expires_at
  • rp_attrs
Special formatting characters are supported. In the default query:
  • %0 is replaced by the SQL quoted context_key.
  • %1 is replaced by the current time as a Unix timestamp.
It defaults to:
SELECT username,peer_ip,peer_port,tacacs_port,tacacs_rem_addr,
       created_at,expires_at,rp_attrs
       FROM TACACS_CONTEXT
       WHERE context_key=%0 AND expires_at >= %1
When one or more GetContextQueryParam parameters are defined, GetContextQuery is used with SQL bind variables instead. Special formatting characters in GetContextQueryParam are replaced before being bound to the SQL query.
For GetContextQueryParam:
  • %0 is the unquoted context_key.
  • %1 is the current time as a Unix timestamp.
For example:
GetContextQuery SELECT username,peer_ip,peer_port,tacacs_port,tacacs_rem_addr, \
                       created_at,expires_at,rp_attrs \
                FROM TACACS_CONTEXT \
                WHERE context_key=? AND expires_at >= ?
GetContextQueryParam %0
GetContextQueryParam %1

3.123.4. DeleteContextQuery and DeleteContextQueryParam

This SQL statement is used to delete a single TACACS+ context from the database by context_key. Unless ReplaceContextQuery is defined, DeleteContextQuery is normally run before AddContextQuery when storing a context. This clears any existing row for the same context_key. Special formatting characters are supported.
In the default query:
  • %0 is replaced by the SQL quoted context_key.
The default query is:
DELETE FROM TACACS_CONTEXT WHERE context_key=%0
When one or more DeleteContextQueryParam parameters are defined, DeleteContextQuery is used with SQL bind variables instead. Special formatting characters in DeleteContextQueryParam are replaced before being bound to the SQL statement.
For DeleteContextQueryParam:
  • %0 is the unquoted context_key.
For example:
DeleteContextQuery DELETE FROM TACACS_CONTEXT WHERE context_key=?
DeleteContextQueryParam %0

3.123.5. ReplaceContextQuery and ReplaceContextQueryParam

This optional SQL statement is used to replace or upsert (insert or update) a TACACS+ context in one database-specific operation. If ReplaceContextQuery is defined, it is used instead of the default DeleteContextQuery and AddContextQuery sequence. This can be more efficient or atomic when the database supports a native replacement or upsert syntax.
Examples of database-specific forms include:
-- SQLite
INSERT OR REPLACE INTO TACACS_CONTEXT
(context_key, username, peer_ip, peer_port, tacacs_port, tacacs_rem_addr,
 created_at, expires_at, rp_attrs)
VALUES (%0,%1,%2,%3,%4,%5,%6,%7,%8)

-- MySQL/MariaDB
REPLACE INTO TACACS_CONTEXT
(context_key, username, peer_ip, peer_port, tacacs_port, tacacs_rem_addr,
 created_at, expires_at, rp_attrs)
VALUES (%0,%1,%2,%3,%4,%5,%6,%7,%8)

-- PostgreSQL
INSERT INTO TACACS_CONTEXT
(context_key, username, peer_ip, peer_port, tacacs_port, tacacs_rem_addr,
 created_at, expires_at, rp_attrs)
VALUES (%0,%1,%2,%3,%4,%5,%6,%7,%8)
ON CONFLICT (context_key) DO UPDATE SET
username=EXCLUDED.username,
peer_ip=EXCLUDED.peer_ip,
peer_port=EXCLUDED.peer_port,
tacacs_port=EXCLUDED.tacacs_port,
tacacs_rem_addr=EXCLUDED.tacacs_rem_addr,
created_at=EXCLUDED.created_at,
expires_at=EXCLUDED.expires_at,
rp_attrs=EXCLUDED.rp_attrs
Special formatting characters are the same as for AddContextQuery. By default, ReplaceContextQuery is not defined.
When one or more ReplaceContextQueryParam parameters are defined, ReplaceContextQuery is used with SQL bind variables instead. Special formatting characters in ReplaceContextQueryParam are replaced before being bound to the SQL statement. The special formatting values are the same as for AddContextQueryParam.

3.123.6. PurgeExpiredQuery and PurgeExpiredQueryParam

This SQL statement is used to delete expired TACACS+ context rows from the database. Special formatting characters are supported.
In the default query:
  • %0 is replaced by the current time as a Unix timestamp.
The default query is:
DELETE FROM TACACS_CONTEXT WHERE expires_at < %0
PurgeExpiredQuery is only run automatically when PurgeInterval is set to a non-zero value. It can be enabled on every Radiator instance that uses the database, or on only some instances. For large databases, it is also possible to run purging from a dedicated instance.
When one or more PurgeExpiredQueryParam parameters are defined, PurgeExpiredQuery is used with SQL bind variables instead. Special formatting characters in PurgeExpiredQueryParam are replaced before being bound to the SQL statement.
For PurgeExpiredQueryParam:
  • %0 is the current time as a Unix timestamp.
For example:
PurgeExpiredQuery DELETE FROM TACACS_CONTEXT WHERE expires_at < ?
PurgeExpiredQueryParam %0

3.123.7. PurgeInterval

This optional parameter specifies the number of seconds between automatic purge runs. If PurgeInterval is unset or set to 0, automatic purging is not scheduled.
# Run purge every 10 minutes
PurgeInterval 600

3.123.8. ContextTimeout

This optional parameter specifies the number of seconds for which stored TACACS+ contexts remain valid in the SQL database. If ContextTimeout is not set, <TACACSContextSQL> uses the AuthorizationTimeout value from its <ServerTACACSPLUS> clause when storing contexts.
ContextTimeout 300

3.123.9. StoreJSON

This optional flag parameter controls how reply attributes are stored. With the default queries these attributes are stored in the rp_attrs column. Customised queries can used the respective % formatter with their definitions. If StoreJSON is enabled and a JSON backend is available, reply attributes are stored as JSON.
The details of formats may change in future versions. At the time of writing the JSON form is an array of attribute name and value pairs. For example:
[["tacacsgroup","group1"]]
If StoreJSON is disabled, or JSON storage is not available, reply attributes are stored in a line based format:
attribute=value
attribute=value