3.18. <SessionDatabase SQL>

This optional clause specifies an external SQL Session Database for radiusd.
SessionDatabase SQL has a number of customisable SQL statements (AddQuery, DeleteQuery, UpdateQuery, ReplaceQuery, ClearNasQuery and CountQuery). These statements are used to add, remove, maintain and count the entries in the SQL Session Database. The default statements will work with the example RADONLINE table in the example SQL schemas in the goodies directory. If you wish, you can use more or fewer columns in your SQL Session Database, and you can change the names of the columns or the table. If you do use a different table schema, you will probably have to change statements to match your schema.
Radiator 4.22 added new SQL statements AddSessionQuery, GetSessionQuery, UpdateSessionQuery and DeleteSessionQuery. See goodies/hotspot.sql for a sample database schema used by goodies/hotspot-fidelio.cfg.
You can configure the SQL database(s) that SessionDatabase SQL uses by defining one or more DBSource, DBUsername and DBAuth lines. For more information about SQL configuration and failure behaviour, see Section 3.8. SQL configuration.
SessionDatabase SQL is tolerant of database failures. If your database server goes down, Radiator will try to reconnect to a database as described above, starting again at the first database you specified. Whichever database Radiator connects to, it will stay connected to it until that database becomes unreachable, at which time it will again search for a database, starting at the first again. If on the other hand, Radiator is not able to connect to any SQL server, it will stop enforcing Simultaneous-Use limits until one of its databases comes back on line.
SessionDatabase SQL understands also the same parameters as SessionDatabase xxxxxx. For more information, see Section 3.17. <SessionDatabase xxxxxx>. It supports also all the common SQL configuration parameters. For more information about the SQL configuration parameters, see Section 3.8. SQL configuration.

3.18.1. SQL Bind Variables

All SessionDatabase SQL statements support SQL bind variables. For more information, see Section 3.8.1. SQL bind variables. An example of DeleteQuery with bind variables is:
DeleteQuery delete from RADONLINE where NASIDENTIFIER =? and NASPORT=?
DeleteQueryParam %1
DeleteQueryParam %2

3.18.2. AddQuery

This SQL statement is executed whenever a new user session starts (i.e. when an Accounting-Request Start message is received). It is expected to record the details of the new session in the SQL database. Special formatting characters may be used. %1 by the NAS IP address, %2 by the NAS-Port, %3 by the SQL quoted Acct-Session-Id. If AddQuery is defined as an empty string, then the query will not be executed.
%0 is replaced by the quoted original user name or rewritten user name, see Section 3.31.23. SessionDatabaseUseRewrittenName.
It defaults to:
insert into RADONLINE (USERNAME, NASIDENTIFIER, NASPORT, 
ACCTSESSIONID, TIME_STAMP, FRAMEDIPADDRESS, NASPORTTYPE, 
SERVICETYPE) values (%0, '%1', %2, %3, %{Timestamp}, 
'%{Framed-IP-Address}', '%{NAS-Port-Type}', '%{Service-Type}')

3.18.3. DeleteQuery

This SQL statement is executed whenever a user session finishes (i.e. when an Accounting-Request Stop message is received). It is expected to remove the details of the session from the SQL database. Special formatting characters may be used. %1 by the NAS IP address, %2 by the NAS-Port, %3 by the SQL quoted Acct-Session-Id and %4 by the Framed-IP-Address. If DeleteQuery is defined as an empty string, then the query will not be executed.
Delete is executed by default when an Access-Request message is received or a new session is added. This attempts to clear any defunct existing session for the port. If the port is not unique for a session, or there's some other reason to control session database updates, see SessionDatabaseOptions.
%0 is formatted as defined in section Section 3.18.2. AddQuery. Section 3.8.1. SQL bind variables are supported.
It defaults to:
delete from RADONLINE where NASIDENTIFIER='%1' and NASPORT=0%2

3.18.4. UpdateQuery

This SQL statement is executed whenever Accounting-Request Alive or Interim-Update message is received. It is expected to update the details of the session in the SQL database. Special formatting characters may be used (the %{attribute} ones are probably the most useful). %1 by the NAS IP address, %2 by the NAS-Port, %3 by the SQL quoted Acct-Session-Id. If UpdateQuery is defined as an empty string, then the query will not be executed and ReplaceQuery, if defined, or AddQuery otherwise, will be used. The default is the empty string.
%0 is formatted as defined in section Section 3.18.2. AddQuery. Section 3.8.1. SQL bind variables are supported.

3.18.5. ClearNasQuery

This SQL statement is executed whenever a NAS reboot is detected. It is expected to clear the details of all sessions on that NAS from the SQL database. Special formatting characters may be used (the %{attribute} ones are probably the most useful). %0 is replaced by the NAS identifier. If ClearNasQuery is defined as an empty string, then the query will not be executed. SQL bind variables are supported.
It defaults to:
delete from RADONLINE where NASIDENTIFIER='%0'

3.18.6. CountQuery

This SQL statement is executed whenever a Simultaneous-Use check item or MaxSessions must be checked during an Access-Request. It is expected to find and return details of all the user sessions currently in the Session Database for the given User-Name. For each entry, it is expected to return the NAS-Identifier, NAS-Port and Acct-Session-Id, IP Address and optionally a user name (in that order) of each session currently in the Session Database. The returned rows are counted, and if there are apparently too many sessions, SessionDatabase SQL will query each NAS and port to confirm if the user is still on line at that port with that session ID. If a user name is present as the fifth field returned by the query, that is the user name that will be used to confirm the user is still on line. If CountQuery is defined as an empty string, then the query will not be executed, and the current session count will be fixed at 0.
%0 is formatted as defined in section Section 3.18.2. AddQuery. %1 replaced by the AuthBy’s DefaultSimultaneousUse, Simultaneous-Use check item or Handler's Max-Sessions value, depending on the context. SQL bind variables are supported.
It defaults to:
select NASIDENTIFIER, NASPORT, ACCTSESSIONID, FRAMEDIPADDRESS \
from RADONLINE where USERNAME=%0
Tip
You can make SessionDatabase SQL count sessions in different ways depending on how you want to restrict your sessions. For example, you could limit the number of users permitted to log in to a particular realm with something like:
CountQuery select NASIDENTIFIER, NASPORT, ACCTSESSIONID, \
      FRAMEDIPADDRESS from RADONLINE where USERNAME like ?
CountQueryParam %%@%R
If your Session Database table included the Called-Station-Id for each session, you could limit the maximum number of users with the same Called-Station-ID with something like:
CountQuery select NASIDENTIFIER, NASPORT, ACCTSESSIONID, FRAMEDIPADDRESS \
           from RADONLINE where CALLEDSTATIONID = ?
CountQueryParam %{Called-Station-Id}

3.18.7. ReplaceQuery

If this optional parameter is defined, it will be used to replace a record in the session database. If it is not defined, DeleteQuery and AddQuery will be used instead. By default, ReplaceQuery is not defined. %1 by the NAS IP address, %2 by the NAS-Port, %3 by the SQL quoted Acct-Session-Id.
%0 is formatted as defined in section Section 3.18.2. AddQuery. Section 3.8.1. SQL bind variables are supported.
This option is provided because some databases (such as MySQL) offer a more efficient way to ‘insert or replace’ queries. Special formatting characters may be used.

3.18.8. CountNasSessionsQuery

This SQL statement is executed whenever Radiator needs the number of sessions currently logged on to a particular NAS. This is only required if HandleAscendAccessEventRequest is defined and an Ascend-Access-Event-Request is received. %1 is replaced by the NAS IP address. SQL bind variables are supported.
It defaults to:
select ACCTSESSIONID from RADONLINE where NASIDENTIFIER='%0'

3.18.9. ClearNasSessionQuery

This SQL statement is executed whenever Radiator needs the number of sessions currently logged on to a particular NAS. This is only required if HandleAscendAccessEventRequest is defined and an Ascend-Access-Event-Request is received and Radiator finds that there is a session in the session database that is not recorded in the NAS. %0 is replaced by the NAS IP address and %1 is replaced by the session ID. SQL bind variables are supported.
It defaults to:
delete from RADONLINE where NASIDENTIFIER='%0' and ACCTSESSIONID = %1

3.18.10. SessionIdentifier

This optional parameter sets the name of a Radius attribute which is used to identify a session. It is useful, for example, when the authentication request contains the session identifier attribute for the subsequent accounting session. The default value is Acct-Session-Id.
# This vendor sends id of subsequent accounting session during authentication
SessionIdentifier Vendor-Session-Id

3.18.11. AddSessionQuery

An SQL statement to add a new session. Special formatting characters may be used. Special variable %2 contains a tenant id, %3 contains object's id and %4 contains object's name. If AddSessionQuery is defined as an empty string, then the query will not be executed.
%0 and %1 are replaced with session parameter names and their values, respectively, and can not currently be used as SQL bind parameters.
AddSessionQuery defaults to:
INSERT INTO SESSIONS (%0) VALUES (%1)

3.18.12. GetSessionQuery

An SQL statement to get a session. Special formatting characters may be used. Special variable %2 contains a tenant id, %3 contains object's id and %4 contains object's name. If GetSessionQuery is defined as an empty string, then the query will not be executed.
%0 and %1 are undefined.
GetSessionQuery defaults to:
SELECT * FROM SESSIONS WHERE tenant_id=%2 AND (name=%4 OR id=%3)

3.18.13. UpdateSessionQuery

An SQL statement to update an existing session. Special formatting characters may be used. Special variable %2 contains a tenant id, %3 contains object's id and %4 contains object's name. If UpdateSessionQuery is defined as an empty string, then the query will not be executed.
%0 contains list of session key=value pairs and can not currently be used as a SQL bind parameter. %1 is undefined.
UpdateSessionQuery defaults to:
UPDATE SESSIONS SET %0 WHERE tenant_id=%2 AND id=%3

3.18.14. DeleteSessionQuery

An SQL statement to delete a session. Special formatting characters may be used. Special variable %2 contains a tenant id, %3 contains object's id and %4 contains object's name. If DeleteSessionQuery is defined as an empty string, then the query will not be executed.
%0 and %1 are undefined.
DeleteSessionQuery defaults to:
DELETE FROM SESSIONS WHERE tenant_id=%2 AND id=%3