Installing with an existing MariaDB server

IDSTower can use a MariaDB server you already have, on the host you install IDSTower on or on another one, and share it with other applications. The installation script asks two things about it, or takes them from its options:

  • Where the server is: on this host, or on another host.

  • Who sets up IDSTower’s login to it: the script, with a database administrator login, or you, with a login a database administrator (DBA) created for IDSTower.

That makes four ways to install:

The script sets up IDSTower’s login

You give the login a DBA created

This host

The script uses the MariaDB it finds here, or installs one, and creates IDSTower’s database and user with an administrator login. This is what a run with no options does.

The script uses the MariaDB already running here, logs in with your login, and creates no user. With no MariaDB here, it stops.

Another host

--database-host. The script creates IDSTower’s database and its 'idstower'@'%' user there, with an administrator login.

--database-host and --database-user. The script logs in with your login and needs no administrator login.

Choosing at the terminal

Run at a terminal, the script asks what no option and no earlier run answers:

Press Enter for this host, or give the database server as <host>[:<port>].
With no MariaDB server running on this host, this script installs one here, unless you then choose a login you created.
Database server [this host]:
Who sets up IDSTower's database login?
  1) This script (needs a database administrator login)
  2) I created one for IDSTower
Choose 1 or 2 [1]:

For the server, press Enter for this host, or give it as a host name, an IPv4 address or, in brackets, an IPv6 address, with its port after a colon when it is not 3306: db.example.com:3307, 10.0.0.5, [2001:db8::5]:3307. Only an IPv6 address takes brackets. localhost, 127.0.0.1 and ::1 name this host. Pressing Enter at both questions installs as every earlier version of the script did: when no MariaDB server runs on this host, the script installs one. With 2 and no MariaDB server here, it installs none and stops, changing nothing.

With 2, the script asks for the login and the name of IDSTower’s database. The password is not shown while you type it and is not written to the installation log:

IDSTower database username [idstower]:
IDSTower database password:
Database name [IDSTower]:

An answer that is not one the script can use is asked again.

Choosing with options

Automation has no terminal to ask at, and neither does a run whose output goes to a file. Such a run asks nothing, and the options make the choices:

  • --database-host <host>[:<port>] names a server on another host. Without it, the server is on this host.

  • --database-user <name>, with the password in the IDSTOWER_DATABASE_PASSWORD environment variable, gives the login a DBA created. Without it, the script sets up IDSTower’s login with an administrator login: --database-admin-user <name> with the IDSTOWER_DATABASE_ADMIN_PASSWORD environment variable, or the one in its credentials file. The two options cannot be given together.

  • --database-name <name> names IDSTower’s database, with either choice: see The database’s name.

There is no option for a password, because a command line can be read by every user of the host. read -rs reads the password without showing it and without leaving it in the shell’s history; type it and press Enter:

$ read -rs IDSTOWER_DATABASE_PASSWORD
$ export IDSTOWER_DATABASE_PASSWORD
$ bash install-IDSTower.sh -i --database-host db.example.com --database-user idstower

sudo does not pass environment variables on by default. To run the script with sudo, name the variable: sudo --preserve-env=IDSTOWER_DATABASE_PASSWORD bash install-IDSTower.sh -i ….

The script itself never puts a password on the command line of a command it runs, because sudo writes each command line to the system’s authentication log and ps shows it to every user of the host while the command runs. The database client reads the password from a file, and the statements that set one on its standard input; openssl reads the certificate’s password from files too. Those files are in a directory only the user running the script can read, and are removed when the run ends, whether it went well or not.

A run with no terminal that is missing something stops before it changes anything, with a message that names the option or the variable to give. A variable for the other choice stops a run too, before it changes anything: IDSTOWER_DATABASE_PASSWORD when the script sets up IDSTower’s login, that is with --database-admin-user, or without --database-user when neither the credentials file nor the answer at the terminal says provided; and IDSTOWER_DATABASE_ADMIN_PASSWORD when IDSTower uses a provided login.

An option answers its question at a terminal too, which is then not asked.

Whichever way the choices were made, the script saves them to its credentials file as soon as the run knows them: the server on the DatabaseHost and DatabasePort lines, the database on DatabaseName, who set up the login on IDSTowerDatabaseLoginOrigin (script or provided), and a provided login on IDSTowerDatabaseUsername and IDSTowerDatabasePassword. A later run reads them and asks nothing it already has, so neither the options nor the variables are needed again, and a run that stopped before IDSTower was installed keeps them too. Once IDSTower is installed, /opt/idstower/appsettings.json holds the server, the database and the login as well.

A re-run over an installed IDSTower without the credentials file, and with none of the options and variables above, does not ask who sets up the login. It tries the login in /opt/idstower/appsettings.json: when the server accepts it and it holds every privilege IDSTower needs (listed under A login a DBA created for IDSTower), the run uses it as a provided login, and creates no user, changes no password and grants no privilege. Otherwise the script sets that login up with an administrator login, as under When the script sets up IDSTower’s login. Without the credentials file, a re-run therefore leaves a working login as it is, also one the script set up: it does not give that login back a privilege beyond those IDSTower needs, such as the grant option the script gives it.

A login a DBA created for IDSTower

When the person who runs the script may not have an administrator login, a DBA creates IDSTower’s login, and the script uses it as it is. These statements create the database and a login for it, where <IDSTower host> is the IDSTower host’s address or name as the server sees it, localhost for a server on the IDSTower host itself, or % for any host:

CREATE DATABASE IDSTower;
CREATE USER 'idstower'@'<IDSTower host>' IDENTIFIED BY '…';
GRANT ALL ON IDSTower.* TO 'idstower'@'<IDSTower host>';

The login may also create the database itself: leave out CREATE DATABASE, and the script creates it with that login, because GRANT ALL ON IDSTower.* covers creating the database of that name:

CREATE USER 'idstower'@'<IDSTower host>' IDENTIFIED BY '…';
GRANT ALL ON IDSTower.* TO 'idstower'@'<IDSTower host>';

IDSTower sets up and upgrades its own tables: it creates, changes and drops tables and indexes, reads and writes their rows, and runs each upgrade step as a procedure it creates, calls and drops again. These are the privileges that takes, on IDSTower’s database only, and the ones the script checks for:

GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER, DROP, INDEX,
      CREATE ROUTINE, ALTER ROUTINE, EXECUTE ON IDSTower.* TO 'idstower'@'<IDSTower host>';

GRANT ALL ON IDSTower.* gives them all, and so keeps working when a later version of IDSTower needs another privilege on its own database.

The user name may be another one, and so may the database’s name, given with --database-name. IDSTower cannot use a password with a double quote, a backslash or a semicolon in it, or one that starts or ends with a single quote or a space, so the script refuses one.

Before it changes anything on the host, the script checks the login, and stops with a message that says what to do when one of these fails:

  • The login: the server accepts it. A refused login is asked for again at a terminal, up to three times, and one that works is saved to the credentials file.

  • The server, on another host: a MariaDB of version 10.6 or newer, as under Versions and other database servers.

  • The database: when it is not there, the script creates it with the login, and when the server refuses that, the script stops and prints the GRANT and CREATE DATABASE statements a DBA runs. A database that is there is used, also when it holds other applications’ tables. A database that already holds IDSTower’s own tables (database_version_history or __EFMigrationsHistory), with no IDSTower installed on this host that uses it, is another IDSTower installation’s: the script stops, because this host does not have that installation’s encryption key and IDSTower could not read what the database holds. To move that installation to this host, copy its /opt/idstower/appsettings.json to the same path here first, or give this IDSTower a database of its own with --database-name. A run over the IDSTower installed on this host goes on with its database.

  • The privileges: the login holds each privilege listed above. The script tries them with the login, in IDSTower’s database, on a table and a procedure named idstower_installer_privilege_check_ followed by random characters: it creates the table, adds a column and an index to it, writes, reads, changes and deletes a row, and drops it, and it drops the procedure if it is there, creates it, calls it and drops it, as an upgrade step does. When the server refuses one of these, the script stops, names the privilege that is missing, and prints the GRANT statement a DBA runs, for the account the server took the login as. A table or procedure it could create but not drop again is named in the message, for the DBA to drop.

With a provided login, the script creates no database user, changes no password, grants no privilege and reloads no privileges, and logs in as no administrator. It needs a MariaDB already installed on this host when the database is on this host: the script installs MariaDB only when it sets up IDSTower’s login itself. It starts mariadb.service if it is stopped, and does not change whether it starts at boot, as when it sets up the login.

The database’s name

IDSTower’s database is IDSTower unless --database-name or the question names another. A name is up to 64 letters, digits and _, and does not start with a digit. The script writes it to /opt/idstower/appsettings.json and to the credentials file, and a later run keeps it. A run whose --database-name names another database than the installed IDSTower uses stops before it changes anything: the script does not move IDSTower’s data to another database. So does a run whose credentials file and installed IDSTower name two different databases.

When the script sets up IDSTower’s login

The script says which server it uses, with its version. Then it:

  • creates IDSTower’s database, IDSTower unless named otherwise

  • creates the idstower database user and gives it all privileges on that database. On this host the user is 'idstower'@'localhost'; on another host it is 'idstower'@'%', see IDSTower’s database user on another host

  • writes the server’s name and port and the database’s name to /opt/idstower/appsettings.json, where IDSTower reads them

It leaves the other databases, the other users and the server’s configuration alone, and the administrator login keeps the password it has. It does not install, upgrade or remove the server’s packages. On this host it also starts mariadb.service if it is stopped, but does not change whether the server starts at boot: when systemd says the server is disabled, the closing message says so and gives sudo systemctl enable mariadb.service, because IDSTower needs its database after a restart. On the RHEL family and Amazon Linux, root keeps its login through the local socket.

With a server on another host, with either choice, the script installs no database server here and looks for none. It needs a MariaDB client on this host for its own statements, and installs one only when there is neither mariadb nor mysql: mariadb-client on Ubuntu, Debian and Kali, mariadb on the RHEL 9 and 10 families and on Fedora, mariadb105 on Amazon Linux 2023. On the RHEL 8 family the client comes from MariaDB’s own 10.6 repository, which the script writes to /etc/yum.repos.d/mariadb.repo.

A run that names another server than the installed IDSTower uses stops before it changes anything: the script does not move IDSTower’s database from one server to another. So does a run whose credentials file and installed IDSTower name two different servers.

The administrator login

When the script sets up IDSTower’s login, it needs an administrator login to create the database and the user. It tries these in order, and asks only at the end:

  1. The login it holds: the one given with --database-admin-user and the IDSTOWER_DATABASE_ADMIN_PASSWORD environment variable, else the one in the credentials file, else the one it generated. A fresh MariaDB from your distribution lets root log in through the local socket whatever password is given, so on this host this is often enough, and nothing is asked.

  2. On this host only, no password at all, which picks up root’s own MariaDB client settings in /root/.my.cnf, when that file holds the password. Those settings are for the server on this host, so they are never sent to a server on another host.

  3. It asks at the terminal, up to three times, for a user name and a password. The password is not shown while you type it and is not written to the installation log. Any characters work. The login IDSTower itself uses, idstower unless the credentials file names another, is not accepted there, because the script sets that login’s password.

    MariaDB administrator username [root]:
    MariaDB administrator password:
    

A login that works is saved to the credentials file, so a later run does not ask again and needs neither the option nor the variable.

A server on another host has to accept the login from the IDSTower host, with either choice. An account such as 'root'@'localhost' takes logins on the database server itself only, so it is refused from anywhere else. Use an account whose host part matches the IDSTower host, '<name>'@'<IDSTower host's address>', with the privileges listed under Privileges the administrator login needs.

When no account on that server is for the IDSTower host, the server refuses the connection before it looks at any login, with MariaDB’s error 1130. The script then stops and says that the server lets no account connect from this host. It does not ask for another login, because the server would refuse that one too. MariaDB’s 10.11 and 11.8 clients, which use TLS by default, print that error inside another one, which reads like a network failure although the server answered:

ERROR 2002 (HY000): Received error packet before completion of TLS handshake. The authenticity of the following error cannot be verified: 1130 - Host '10.0.0.7' is not allowed to connect to this MariaDB server

An administrator of that server has to create an account the IDSTower host may use, as above, before the script can go on.

Without a terminal, a run whose administrator logins are all refused stops before it creates any database or user, with a message that names the server:

The database server on 'db.example.com:3306' refused the administrator login 'root'.

Give the script the login beforehand, with --database-admin-user and the IDSTOWER_DATABASE_ADMIN_PASSWORD environment variable, as under Choosing with options:

$ read -rs IDSTOWER_DATABASE_ADMIN_PASSWORD
$ export IDSTOWER_DATABASE_ADMIN_PASSWORD
$ bash install-IDSTower.sh -i --database-host db.example.com --database-admin-user dbadmin

The option and the variable take the place of the login in the credentials file for that run, and a login that works is saved to it, as a typed one is.

The credentials file

A login can also be written into the credentials file IMPORTANT_idstower_installation_script_generated_credentials before a run. The script writes that file to the directory you run it from, or to your home directory if that one is not writable; for a first run, create it there yourself. For an administrator login, set DatabaseAdminUsername and DatabaseAdminPassword; for a login a DBA created, set IDSTowerDatabaseUsername, IDSTowerDatabasePassword and IDSTowerDatabaseLoginOrigin=provided. Leave the other lines as they are:

DatabaseAdminUsername=root
DatabaseAdminPassword=Real$Pass word&1

The script reads the file as plain NAME=value lines and keeps the password as you wrote it on every later run, with no quoting needed whatever characters it holds. A pair of quotes around the whole value is taken off, and so are spaces and tabs at the end of a line. A password that really ends in a space goes in single quotes: DatabaseAdminPassword='Real pass '. A password you chose is not printed in the list of credentials the script shows, and so does not reach the installation log.

Note

Installation script versions before 1.7 run this file as a shell script. A password holding $, a space or & has to be wrapped in single quotes there:

DatabaseAdminPassword='Real$Pass word&1'

Those versions rewrite the file without the quotes, so add them again before each re-run.

Versions and other database servers

IDSTower needs MariaDB 10.6 or newer. On an older MariaDB the script stops and names the version it found. It does not offer to upgrade the server, because other applications may depend on it.

IDSTower runs on MariaDB only. On a server on another host that turns out to be MySQL or Percona Server, the script stops before it creates anything. On this host it stops before it runs the package manager for the database, because MariaDB cannot be installed next to them. This is what the package manager does beside version 8.0 of either:

  • On Ubuntu 24.04, apt-get install mariadb-server removes the server without asking. For MySQL it also moves the data directory aside to /var/lib/mysql-8.0, and the new MariaDB server starts without those databases.

  • On Rocky Linux 9, dnf install mariadb-server refuses with a package conflict beside MySQL. Beside Percona Server it says Nothing to do and installs no MariaDB.

Use another host for IDSTower in that case, or a MariaDB server on another host.

Privileges the administrator login needs

The administrator login does not have to be root. It needs these privileges, with IDSTower’s database name in place of IDSTower when you named another:

GRANT CREATE USER, RELOAD ON *.* TO <account>;
GRANT ALL ON IDSTower.* TO <account> WITH GRANT OPTION;

To check a login, connect with it and run:

SHOW GRANTS FOR CURRENT_USER;

For root, look at the GRANT ALL PRIVILEGES ON *.* line in particular: that line has to end with WITH GRANT OPTION. MariaDB also prints a second line, GRANT PROXY ON ''@'%' TO 'root'@'localhost' WITH GRANT OPTION, even for a root that has lost its grant option, so seeing those words somewhere in the output proves nothing.

A root whose GRANT ALL PRIVILEGES ON *.* line ends without WITH GRANT OPTION can log in and create users, but gets ERROR 1044 at the script’s GRANT step. It cannot repair itself, because giving the option back takes the option: see Restoring the grant option when there is no other administrator.

A login that lacks a privilege is found at the statement that needs it. The script stops there and prints the two GRANT lines above.

IDSTower’s database user on another host

When the script sets up IDSTower’s login on a server on another host, IDSTower’s user is 'idstower'@'%': the server accepts its login from any address it lets connect, not only from the IDSTower host. That keeps working when the IDSTower host’s address changes or reaches the server through NAT. Its password is a long random one the script generated, kept in the credentials file and in /opt/idstower/appsettings.json. Let only the hosts that need the server reach its port, with a firewall in front of it.

Each IDSTower installation needs a database of its own. Before it changes anything on a server on another host, the script checks IDSTower’s database there as it does for a provided login: a database that holds IDSTower’s own tables is another installation’s, unless the IDSTower installed on this host uses it or IDSTower’s user there accepts the login in the credentials file, which shows that an earlier run on this host made them. An empty database, as a DBA creates for IDSTower, is used. The script also looks for the 'idstower'@'%' user: when it is there and refuses the login in the credentials file, another IDSTower installation uses it. In either case the script stops and leaves the server alone. Going on would give that installation’s user a new password, which locks that installation out, and the new host’s encryption key could not read what the database holds.

  • To move an IDSTower installation to a new host, copy its /opt/idstower/appsettings.json, which holds its encryption key, its database login and its server, to the same path on the new host before you run the script there. If the script already ran on the new host, move the credentials file it wrote there out of the way as well.

  • Otherwise give the new IDSTower another server, or another database with --database-name.

An administrator login with only the privileges listed under Privileges the administrator login needs cannot read the server’s accounts, so with such a login the script finds another installation by its tables only.

A server on another host has to listen on the network. If the script cannot reach it, check that the server’s name resolves on the IDSTower host, that no firewall between the two blocks its port, and that the server’s configuration sets neither skip-networking nor a bind-address that names its own loopback address only. The script waits 10 seconds for each connection to such a server before it says it cannot reach it.

Encryption of the connection

IDSTower encrypts its connection to the database server with TLS when the server offers it, as MariaDB 11.4 and newer do without being configured to. It does not check the server’s certificate: it connected over TLS to a MariaDB 11.8 server holding the certificate that server made for itself, which nothing on the IDSTower host trusts. The connection is therefore protected against someone who reads the network, not against someone who can pose as the server. To a server that offers no TLS, IDSTower connects unencrypted.

The script’s own statements go through the database client on the IDSTower host, and it connects as IDSTower does: encrypted when the server offers TLS, without checking the certificate. It gives MariaDB’s client --ssl --disable-ssl-verify-server-cert for that, with an administrator login and with a provided login. Without those options, the clients of MariaDB 11.4 and newer, which Debian 13, Ubuntu 26.04, Kali and Fedora install, refuse a server that offers no TLS and one whose certificate the IDSTower host does not trust:

ERROR 2026 (HY000): TLS/SSL error: SSL is required, but the server does not support it
ERROR 2026 (HY000): TLS/SSL error: Certificate verification failure: The certificate is NOT trusted.

The older ones, such as MariaDB 10.6 on Ubuntu 22.04 and the RHEL 8 family and 10.5 on Amazon Linux 2023, connect unencrypted without them. MySQL’s client encrypts when the server offers TLS without being told to. To a server that offers no TLS, the statement that sets IDSTower’s password crosses the network unencrypted, as IDSTower’s own login does then. Give that server TLS, or run the script where the network between the two hosts is trusted.

Do not remove MariaDB to get past a refused login

Warning

Do not remove MariaDB to get past a refused login if anything else uses it. apt-get purge mariadb-server alone keeps /var/lib/mysql, so the old password survives a reinstall and the script asks in the same place. Deleting that directory deletes every database on the server.

If the client reports unknown variable 'bind-address=...'

bind-address belongs under [mysqld] in the server’s configuration. Under [client-server] or [client] the client reads it too and stops with unknown variable 'bind-address=...'. Move the line under [mysqld] and run the script again.

If a web server holds port 80 or 443

IDSTower listens on port 443, and on port 80 to redirect to it. If a web server on the same host holds either one, give IDSTower another port, on every run of the script:

$ bash install-IDSTower.sh -i -n <hostname>:<port>

IDSTower then listens on that port alone. The script only opens ports 80 and 443 in the firewall, so the port you choose is yours to open.

Restoring the grant option when there is no other administrator

Warning

Use this only on a host with no other administrator login. These commands restart MariaDB twice, so every application using the server loses its connection. While the server runs without its grant tables anyone on the host can log in with every privilege, and --skip-networking keeps it off the network during that time.

On the host that runs the MariaDB server:

$ sudo systemctl set-environment MYSQLD_OPTS="--skip-grant-tables --skip-networking"
$ sudo systemctl restart mariadb
$ sudo mariadb -e "FLUSH PRIVILEGES; GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION;"
$ sudo systemctl unset-environment MYSQLD_OPTS
$ sudo systemctl restart mariadb