четверг, 14 февраля 2008 г.

mod_auth_mysql & .htaccess

1. install (FreeBSD)
# cd /usr/ports && make search key="mod_auth_mysql"
select port for your version apache&mysql

# cd /usr/ports/www/mod_auth_mysqlxxx


# make install clean

2. configure
edit httpd.conf (uncomment LoadModule auth_mysql_module libexec/apache22/mod_auth_mysql.so)

create DB&tables (source)
#
# Table structure for table `host_info`
#
# the fields created, updated, and isadmin are not needed by the module!
# they may help you creating a php-htpasswd frontend
#

CREATE TABLE host_info (
id int(14) NOT NULL auto_increment,
host char(255) NOT NULL default '',
host_group int(14) NOT NULL default '0',
created timestamp(14) NOT NULL,
updated timestamp(14) NOT NULL,
PRIMARY KEY (id),
KEY host (host)
) TYPE=MyISAM PACK_KEYS=1;
# --------------------------------------------------------

#
# Table structure for table `user_group`
#

CREATE TABLE user_group (
id int(14) NOT NULL auto_increment,
user_name char(50) NOT NULL default '',
user_group char(20) NOT NULL default '',
host_group int(14) default NULL,
created timestamp(14) NOT NULL,
updated timestamp(14) NOT NULL,
PRIMARY KEY (id),
KEY host_group (host_group),
KEY user_group (user_group)
) TYPE=MyISAM PACK_KEYS=1;
# --------------------------------------------------------

#
# Table structure for table `user_info`
#

CREATE TABLE user_info (
id int(14) NOT NULL auto_increment,
user_name char(30) NOT NULL default '',
user_passwd char(120) NOT NULL default '',
host_group int(14) NOT NULL default '0',
created timestamp(14) NOT NULL,
updated timestamp(14) NOT NULL,
isadmin tinyint(4) NOT NULL default '0',
PRIMARY KEY (id),
UNIQUE KEY user_name (user_name,host_group)
) TYPE=MyISAM PACK_KEYS=1;

create .htaccess (source)

AuthName "Protected Area"

#!! If you use this module with apache 2.2.x you
#!! need to uncomment following line !
AuthBasicAuthoritative Off

AuthType Basic

AuthMySQLHost localhost
AuthMySQLUser user
AuthMySQLPassword passwd
AuthMySQLDB dbname
# AuthMySQLPort 3306

#AuthMySQLUserTable user_info
#AuthMySQLTableUserName user_name
#AuthMySQLTableUserPasswd user_passwd
#AuthMySQLTableUserHostGroup host_group

# AuthMySQLHostTable host_info
# AuthMySQLTableHostName host
# AuthMySQLTableHostHostGroup host_group

# AuthMySQLGroupTable user_group
# AuthMySQLTableGroupName user_name
# AuthMySQLTableGroupGroupName user_group
# AuthMySQLTableGroupHostGroup host_group

# AuthMySQLUserQueryCondition 1 // rev 1.8
# AuthMySQLGroupQueryCondition 1 // rev 1.8

AuthMySQLAuthoritative On
AuthMySQLKeepAlive Off
AuthMySQLEnable On
AuthMySQLVirtualHost Off

# AuthMySQLClientUseSSL On // rev 1.10
# AuthMySQLClientCert "full_path/client-cert.pem" // rev 1.10
# AuthMySQLClientKey "full_path/client-key.pem" // rev 1.10
# AuthMySQLClientCA "full_path/cacert.pem" // rev 1.10
# AuthMySQLClientCipher "" // rev 1.10
# AuthMySQLClientVerifyCert Off // rev 1.10

require valid-user

edit this .htaccess for your parameters

fill tables with data (source)
minimum example
insert into host_info(id,host,hostgroup)
values ( null, "www.foo.com", 1 );

insert into host_info(id,host,hostgroup)
values ( null, "www.bar.com", 2 );

insert into user_info (id, user_name,user_passwd, host_group)
values (null,"myself", encrypt("secret"),1);

insert into user_info (id, user_name,user_passwd, host_group)
values (null,"myself", encrypt("password"),2);

restart apache
# apache2 restart

see error logs (no errors)

3. work

username - $_SERVER["PHP_AUTH_USER"]
password - $_SERVER["PHP_AUTH_PW"]


official site

Комментариев нет: