Questa guida spiega come proteggere con password le directory web (con utenti da un database MySQL) con mod_authn_dbd su Apache2 su un server Debian 8 (Jessie). È un'alternativa ai file di password in testo semplice forniti da mod_auth e consente di utilizzare la normale sintassi SQL per creare/modificare elimina utenti. Puoi anche configurare mod_authn_dbd per eseguire l'autenticazione su una tabella utente MySQL esistente. Apache mod_authn_dbd sostituisce mod_auth_mysql.
1 Nota preliminare
Uso vhost http://www.example.com qui con il file di configurazione vhost /etc/apache2/sites-available/example.com.vhost e la radice del documento /var/www/www.example.com/web. Voglio proteggere con password la directory /var/www/example.com/web/protecteddir in questo tutorial (si traduce in http://www.example.com/protecteddir/).
Puoi utilizzare questo tutorial per il server LAMP di base se non hai ancora installato Apache.
2 Installazione di MySQL o MariaDB
Userò MariaDB, un fork di MySQL qui invece di MySQL. Ma MySQL funziona anche se lo preferisci. Per installare MariaDB, eseguiamo:
apt-get -y install mariadb-server mariadb-client
Ti verrà chiesto di fornire una password per l'utente root MySQL:
Nuova password per l'utente "root" di MariaDB:<-- yourrootsqlpassword
Ripeti la password per l'utente "root" di MariaDB:<-- yourrootsqlpassword
Installa il modulo DBD MySQL:
apt-get install libaprutil1-dbd-mysql
Successivamente, abilita il modulo mod_authn_dbd:
a2enmod dbd
a2enmod authn_dbd
authn_socache
Riavvia Apache:
service apache2 restart
3 Configurazione di mod_authn_dbd
Puoi trovare la documentazione per mod_authn_dbd nella documentazione di Apache qui http://httpd.apache.org/docs/current/mod/mod_authn_dbd.html.
Dopo aver letto questi due file, creiamo un database MySQL chiamato examplecomdb in cui creeremo la tabella mysql_auth che conterrà i nostri utenti e password. In aggiunta a ciò, creiamo l'utente MySQL examplecom_admin - questo utente verrà utilizzato da mod_auth_mysql per connettersi a MySQL in seguito:
mysqladmin -u root -p create examplecomdb
mysql -u root -p
GRANT SELECT, INSERT, UPDATE, DELETE ON examplecomdb.* TO 'examplecom_admin'@'localhost' IDENTIFIED BY 'examplecom_admin_password';
GRANT SELECT, INSERT, UPDATE, DELETE ON examplecomdb.* TO 'examplecom_admin'@'localhost.localdomain' IDENTIFIED BY 'examplecom_admin_password';
FLUSH PRIVILEGES;
(Sostituisci examplecom_admin_password con una password a tua scelta.)
USE examplecomdb;
create table mysql_auth (
username varchar(255) not null,
passwd varchar(255),
groups varchar(255),
primary key (username)
);
(Naturalmente, puoi anche utilizzare le tabelle esistenti che contengono le tue credenziali utente e puoi anche avere campi aggiuntivi nella tabella, come un campo che definisce se un utente è attivo o meno, ad esempio.)
Ora inseriamo lo user test nella nostra tabella mysql_auth con il password test; questo utente appartiene al gruppo di test.
La password deve essere hash, userò un hash SHA1 qui, l'hash può essere creato con il comando htpasswd sulla shell Linux:
htpasswd -bns test test
Il risultato è questo:
test:{SHA}qUqP5cyxm6YcTAhz05Hph5gvu9M=
La prima parte è il nome utente "test", separato da ":" e poi arriva la password con hash. Abbiamo bisogno della password hash "{SHA}qUqP5cyxm6YcTAhz05Hph5gvu9M=" solo per inserirla nel nostro database utente. La query MySQL è questa:
INSERT INTO `mysql_auth` (`username`, `passwd`, `groups`) VALUES('test', '{SHA}qUqP5cyxm6YcTAhz05Hph5gvu9M=', 'testgroup');
Quindi lasciamo la shell MySQL:
quit
La configurazione per mod_authn_dbd da includere nel file vhost, potrebbe non essere aggiunta all'interno di un file .htaccess. Pertanto modifichiamo il file vhost e aggiungiamo la seguente configurazione alla fine del file:
nano /etc/apache2/sites-available/example.com.vhost
[...] # mod_dbd configuration
DBDriver mysql
DBDParams "dbname=examplecomdb user=examplecom_admin pass=examplecom_admin_password"
DBDMin 4
DBDKeep 8
DBDMax 20
DBDExptime 300
<Directory "/var/www/example.com/web/protecteddir">
# mod_authn_core and mod_auth_basic configuration
# for mod_authn_dbd
AuthType Basic
AuthName "My Server"
# To cache credentials, put socache ahead of dbd here
AuthBasicProvider socache dbd
# Also required for caching: tell the cache to cache dbd lookups!
AuthnCacheProvideFor dbd
AuthnCacheContext my-server
# mod_authz_core configuration
Require valid-user
# mod_authn_dbd SQL query to authenticate a user
AuthDBDUserPWQuery "SELECT passwd FROM mysql_auth WHERE username = %s"
</Directory>
Ricarica Apache:
service apache2 reload
Se hai campi aggiuntivi nella tua tabella MySQL che definiscono se un utente può accedere o meno (ad esempio un campo chiamato attivo), puoi aggiungerlo alla query utente SQL in questo modo:
[...] AuthDBDUserPWQuery "SELECT passwd FROM mysql_auth WHERE username = %s and active = 'yes'" [...]
La direttiva require valid-user fa in modo che ogni utente elencato nella tabella mysql_auth possa accedere purché fornisca la password corretta. Se desideri che solo determinati utenti possano accedere, dovresti utilizzare qualcosa come
[...] require user jane joe [...]
invece. E se desideri che solo i membri di determinati gruppi possano accedere, dovresti utilizzare qualcosa del genere:
[...] require group testgroup [...]
Questo è tutto! Ora prova ad accedere a http://www.example.com/protecteddir/ e ti dovrebbero essere richiesti un nome utente e una password:
4 link
- Apache:http://httpd.apache.org/
- Debian:http://www.debian.org/
- mod_authn_dbd: http://httpd.apache.org/docs/current/mod/mod_authn_dbd.html