| 1 |
= Communicating with an SQL database |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
*Goal:* To configure the server to communicate with an SQL database. |
| 6 |
|
| 7 |
*Time:* 25-40 minutes. |
| 8 |
|
| 9 |
*File:* |
| 10 |
|
| 11 |
- `etc/raddb/mods-available/sql` |
| 12 |
- `etc/raddb/mods-config/sql/main/*` |
| 13 |
|
| 14 |
In addition to the file, the server may obtain user configuration |
| 15 |
information from an SQL database. In this exercise, you will |
| 16 |
configure the server to communicate with an SQL database. you |
| 17 |
will configure the schema for the SQL server, and will populate that |
| 18 |
schema with a sample entry similar to that for the exercise in |
| 19 |
xref:new_user.adoc[New User]. |
| 20 |
|
| 21 |
There are a number of reasons why user information may be stored in an |
| 22 |
SQL database, rather than the file. While the file is adequate for a |
| 23 |
small number of users, it does not scale well to millions of users, and |
| 24 |
it may not be updated by general non-RADIUS tools. In contrast, SQL |
| 25 |
databases are designed to store millions of records, to have fast |
| 26 |
queries and retrievals, and there are many third-party tools for |
| 27 |
maintaining and updating the databases. |
| 28 |
|
| 29 |
The SQL schema used by FreeRADIUS is designed to mirror the users file. |
| 30 |
Each SQL dialect has its own set of schema and configuration files. |
| 31 |
They are located in the `raddb/mods-config/sql/main/<dialect>` directory. |
| 32 |
|
| 33 |
The schema is defined by the "schema.sql" file, and the queries are |
| 34 |
defined by the `queries.conf` file. |
| 35 |
|
| 36 |
The main configuration for the SQL module is `raddb/mods-available/sql` |
| 37 |
it will `$INCLUDE` the appropriate "queries.conf" file for the dialect |
| 38 |
chosen. |
| 39 |
|
| 40 |
You should now read the configuration file for the SQL database which you |
| 41 |
use. |
| 42 |
|
| 43 |
[TIP] |
| 44 |
======================================================================== |
| 45 |
Unless there is a pre-configured database available we recommend the |
| 46 |
sqlite driver be used. If the sqlite specific stanzas are uncommented |
| 47 |
in `raddb/mods-available/sql` it will automatically bootstrap a new |
| 48 |
database using the bundled schema. |
| 49 |
======================================================================== |
| 50 |
|
| 51 |
The first step is to configure the server to use the SQL module, and to |
| 52 |
communicate with the SQL database. Edit the `radiusd.conf` file, and |
| 53 |
look for the `instantiate` section. Inside of that section, add one line |
| 54 |
containing the word `sql`, as follows: |
| 55 |
|
| 56 |
------------------------------------------ |
| 57 |
instantiate { |
| 58 |
sql # start up the SQL module. |
| 59 |
} |
| 60 |
------------------------------------------ |
| 61 |
|
| 62 |
This tells the server to look for, and use, the `sql` module when the |
| 63 |
server starts. Note that since the `sql` module is not listed in any of |
| 64 |
the "authorize", "authenticate", etc. sections, it will not be used |
| 65 |
in to process any authentication requests, or accounting requests. For |
| 66 |
now, we are interested solely in making the FreeRADIUS server |
| 67 |
communicate with the SQL server. |
| 68 |
|
| 69 |
Open `raddb/mods-available/sql` verify that the first few configuration |
| 70 |
entries are correct. That is, the "server", "login", and "password" |
| 71 |
entries should be set up correctly for your local SQL database. |
| 72 |
|
| 73 |
If using sqlite, these entries can be left commented out as they're not |
| 74 |
required, but you should uncomment the `sqlite {}` section and the |
| 75 |
configuration items within that section. |
| 76 |
|
| 77 |
FreeRADIUS uses the `rlm_sql` module to interface with the SQL |
| 78 |
databases. You should check that this module is installed by doing: |
| 79 |
|
| 80 |
[source, bash] |
| 81 |
-------------------------------------- |
| 82 |
$ ls -l usr/lib64/freeradius/rlm_sql* |
| 83 |
-------------------------------------- |
| 84 |
|
| 85 |
(Or, the directory wherever the FreeRADIUS libraries were installed.) |
| 86 |
|
| 87 |
You should see not only files like "rlm_sql.so", but also |
| 88 |
"rlm_sql_mysql.so", or "rlm_sql_sqlite.so". If you do not see |
| 89 |
"rlm_sql.so", or you do not see the sub-module which interfaces with |
| 90 |
your SQL server, you will have to install it now. You may do this by |
| 91 |
going to the rlm_sql subdirectory, and building the module: |
| 92 |
|
| 93 |
[source, bash] |
| 94 |
------------------------ |
| 95 |
$ cd freeradius-server/src/modules/rlm_sql |
| 96 |
$ ./configure |
| 97 |
$ make |
| 98 |
$ make install |
| 99 |
------------------------ |
| 100 |
|
| 101 |
This exercise has insufficient room to describe how to debug any |
| 102 |
configuration, build, or installation problems with the SQL drivers. For |
| 103 |
the remainder of this exercise, we will assume that the driver is |
| 104 |
installed in the appropriate library directory. |
| 105 |
|
| 106 |
Once you have verified that the SQL driver exists, have enabled the module |
| 107 |
by creating a symlink from `raddb/mods-available/sql` to |
| 108 |
`raddb/mods-enabled/sql` and you have configured the appropriate sql dialect, |
| 109 |
you should start the server as usual: |
| 110 |
|
| 111 |
------------ |
| 112 |
$ radiusd -X |
| 113 |
------------ |
| 114 |
|
| 115 |
If all has gone well, the server should print out the normal "Ready to |
| 116 |
process requests" message. Scroll up in your terminal window, and there |
| 117 |
should be messages from the `sql` module, such as: |
| 118 |
|
| 119 |
-------------- |
| 120 |
rlm_sql_sqlite: Database doesn't exist, creating it and loading schema |
| 121 |
rlm_sql_sqlite: Executing SQL statements from file "/etc/raddb/mods-config/sql/main/sqlite/schema.sql" |
| 122 |
rlm_sql (sql): Driver rlm_sql_sqlite (module rlm_sql_sqlite) loaded and linked |
| 123 |
rlm_sql (sql): Attempting to connect to database "radius" |
| 124 |
rlm_sql (sql): Initialising connection pool |
| 125 |
pool { |
| 126 |
start = 5 |
| 127 |
min = 4 |
| 128 |
max = 100 |
| 129 |
spare = 3 |
| 130 |
uses = 0 |
| 131 |
lifetime = 0 |
| 132 |
cleanup_interval = 30 |
| 133 |
idle_timeout = 60 |
| 134 |
retry_delay = 1 |
| 135 |
spread = no |
| 136 |
} |
| 137 |
rlm_sql (sql): Opening additional connection (0) |
| 138 |
rlm_sql_sqlite: Opening SQLite database "/tmp/freeradius.db" |
| 139 |
rlm_sql (sql): Opening additional connection (1) |
| 140 |
rlm_sql_sqlite: Opening SQLite database "/tmp/freeradius.db" |
| 141 |
rlm_sql (sql): Opening additional connection (2) |
| 142 |
rlm_sql_sqlite: Opening SQLite database "/tmp/freeradius.db" |
| 143 |
rlm_sql (sql): Opening additional connection (3) |
| 144 |
rlm_sql_sqlite: Opening SQLite database "/tmp/freeradius.db" |
| 145 |
rlm_sql (sql): Opening additional connection (4) |
| 146 |
rlm_sql_sqlite: Opening SQLite database "/tmp/freeradius.db" |
| 147 |
-------------- |
| 148 |
|
| 149 |
These messages indicate that the server was able to load the `sql` |
| 150 |
module, and that the `sql` module was able to communicate with the SQL |
| 151 |
server. |
| 152 |
|
| 153 |
If there is a problem with shared libraries, or with access permissions |
| 154 |
to the SQL database, then an error message will be printed, and the |
| 155 |
server will not start properly. The FreeRADIUS FAQ and the |
| 156 |
`radiusd.conf` file, entry "libdir", contain information as to how to |
| 157 |
fix shared library issues. |
| 158 |
|
| 159 |
If there are issues connecting to the database you should verify manually |
| 160 |
that you can connect to the SQL database using the given "server", |
| 161 |
"login", and "password". The SQL database should come with a test |
| 162 |
client which may be used to perform this test. |
| 163 |
|
| 164 |
Now stop the server. The next exercise will be to add the schema to the |
| 165 |
database, and to populate it with a test entry. |
| 166 |
|
| 167 |
== Questions |
| 168 |
|
| 169 |
1. Why is it important to test SQL connectivity, independently of |
| 170 |
testing the ability to obtain user configuration from an SQL database? |
| 171 |
2. Why are there different configuration files for each SQL server? |
| 172 |
3. What additional benefits, not mentioned here, do SQL databases have |
| 173 |
over the files module? |
| 174 |
|
| 175 |
// Copyright (C) 2021 Network RADIUS SAS. Licenced under CC-by-NC 4.0. |
| 176 |
// This documentation was developed by Network RADIUS SAS. |