# /etc/init.d/apache2 test
Check for errors, and if OK:
# /etc/init.d/apache2 restart
Then:
# grep dbd /etc/apache2/sysconfig.d/loadmodule.conf
And you should see:
LoadModule dbd_module /usr/lib64/apache2-prefork/mod_dbd.so
LoadModule authn_dbd_module /usr/lib64/apache2-prefork/mod_authn_dbd.so
If you’re not using SuSe, check your httpd.conf for similar entries, and add if lacking.
# mysql -uroot -p
Passowrd: xxxxx #Replace with you own MySQL root password
SHOW databases;
CREATE database apache;
SHOW databases;
USE apache;
CREATE TABLE USRS (UsrID VARCHAR(16), Passwd VARCHAR(64), PRIMARY KEY (UsrID));
CREATE TABLE GRPS (Grp VARCHAR(16), PRIMARY KEY (Grp));
CREATE TABLE USRS_GRPS (Grp VARCHAR(16), UsrID VARCHAR(16), PRIMARY KEY (Grp, UsrID));
SHOW tables;
CREATE USER apacheBR@localhost IDENTIFIED BY 'some_pass';
GRANT SELECT
ON apache.*
TO apacheBR@localhost;
CREATE USER maintScript@localhost IDENTIFIED BY 'other_pass';
GRANT SELECT,INSERT,UPDATE,DELETE
ON apache.*
TO maintScript@localhost;
FLUSH PRIVILEGES;
QUIT
htpasswd2 –bns <username> <password>
calls should suffice e.g.
# htpasswd2 -bns aTestUser aPassW0rd
aTestUser:{SHA}z2DI9nDATSY62mOI/GS4kz4VC10=
# htpasswd2 -bns aDeadUser anOthrPW
imTheAdmin:{SHA}2Vwc0dzvxZ8mblHnBQ3E5nWDxcc=
# htpasswd2 -bns imTheAdmin theAdminPasswd
imTheAdmin:{SHA}+LwfoB15Qfql+EP/WQp9/vzPN9s=
Note: The SuSe supplied script is
htpasswd2 not
htpasswd
# mysql -u maintScript -p'other_pass'
USE apache;
INSERT INTO USRS (UsrID, Passwd) VALUES ('aTestUser', '{SHA}z2DI9nDATSY62mOI/GS4kz4VC10=');
INSERT INTO USRS (UsrID, Passwd) VALUES ('aDeadUser', '{SHA}2Vwc0dzvxZ8mblHnBQ3E5nWDxcc=');
INSERT INTO USRS (UsrID, Passwd) VALUES ('imTheAdmin', '{SHA}+LwfoB15Qfql+EP/WQp9/vzPN9s=');
INSERT INTO GRPS (GRP) VALUES ('Administrator');
INSERT INTO GRPS (GRP) VALUES ('ActiveUser');
INSERT USRS_GRPS (GRP, UsrID) VALUES ('Administrator', 'imTheAdmin');
INSERT USRS_GRPS (GRP, UsrID) VALUES ('ActiveUser', 'aTestUser');
COMMIT;
QUIT;
Next, test the read-only
apache user-id by typing;
# mysql -u apacheBR -p'some_pass';
USE apache;
SELECT * from USRS, USRS_GRPS where USRS.UsrID=USRS_GRPS.UsrID and GRP='Administrator';
QUIT:
# mkdir -p /srv/www/htdocs/RegisterdUser/ActiveMembers
# mkdir -p /srv/www/htdocs/RegisterdUser/AdminOnly
# echo "<HTML><BODY>I'm a Registered user!!!</BODY></HTML>" > /srv/www/htdocs/RegisterdUser/index.html
# echo "<HTML><BODY>I'm an ACTIVE registered user!!!</BODY></HTML>" > /srv/www/htdocs/RegisterdUser/ActiveMembers/index.html
# echo "<HTML><BODY>I'm the Admin :)</BODY></HTML>" > /srv/www/htdocs/RegisterdUser/AdminOnly/index.htm
Then verify that you can get to each URL, via a browser e.g.
# cd /etc/apache2/conf.d
# cp playsite.conf playsite.conf.OLD
Next, edit the file, in a text editor of your choice, and insert the following:
<IfModule mod_dbd.c>
DBDriver mysql
DBDParams "host=127.0.0.1 dbname=apache user=apacheBR pass=some_pass"
DBDMin 1
DBDKeep 8
DBDMax 20
DBDExptime 300
</IfModule>
Note: the Following can be included in the same *.conf file, which I have done, or appended into a
.htaccess file in the respective directory:
<Directory "/srv/www/htdocs/RegisterdUser">
Options FollowSymLinks Indexes MultiViews
AuthType Basic
AuthName "Registered User"
AuthBasicProvider dbd
AuthDBDUserPWQuery "SELECT Passwd FROM USRS WHERE UsrID = %s"
Require valid-user
AllowOverride None
Order allow,deny
Allow from all
</Directory>
<Directory "/srv/www/htdocs/RegisterdUser/ActiveMembers">
Options FollowSymLinks Indexes MultiViews
AuthType Basic
AuthName "Active users Only"
AuthBasicProvider dbd
AuthDBDUserPWQuery "SELECT Passwd from USRS, USRS_GRPS where USRS.UsrID=%s AND USRS.UsrID=USRS_GRPS.UsrID"
Require valid-user
AllowOverride None
Order allow,deny
Allow from all
</Directory>
<Directory "/srv/www/htdocs/RegisterdUser/AdminOnly">
Options FollowSymLinks Indexes MultiViews
AuthType Basic
AuthName "Admin users Only"
AuthBasicProvider dbd
AuthDBDUserPWQuery "SELECT Passwd from USRS, USRS_GRPS where USRS.UsrID=%s AND USRS.UsrID=USRS_GRPS.UsrID AND GRP='Administrator'"
Require valid-user
AllowOverride None
Order allow,deny
Allow from all
</Directory>
# /etc/init.d/apache2 test
Check for / resolve any errors then type:
# /etc/init.d/apache2 restart
[STEP title="Test the Authentication"][/STEP]
# tail /var/log/apache2/error_log
... Internal error: DBD: Can't connect to mysql
If an error is present check you can connection to mysql using the credentials in the
DBDParams entry e.g.
# mysql -h 127.0.0.1 -u apacheBR -psome_pass
7.1) Once logged in as ‘aDeadUser’ attempt to access the following URL’s:
[Tue Jun 15 16:11:45 2010] [error] [client 127.0.0.1] user aDeadUser not found: /RegisterdUser/ActiveMembers/index.html
[Tue Jun 15 16:11:51 2010] [error] [client 127.0.0.1] user aDeadUser not found: /RegisterdUser/ActiveMembers/index.html
Close the Browser, re-open it and browse back to:
[Tue Jun 15 16:10:14 2010] [error] [client 127.0.0.1] user aTestUser not found: /RegisterdUser/AdminOnly/index.html
Finally close and re-open the browser and browse back to the first URL and log in as:
<?php
// Connect to the Apache Authentication DB
$dbhost="localhost";
$username="maintScript";
$password="other_pass";
$database="apache";
mysql_connect($dbhost,$username,$password) or die(mysql_error());
mysql_select_db($database) or die( "Unable to select database: $database".mysql_error());
// ----------------------------
// functions
// ----------------------------
// Insert the New User into the DB
function addUser ($userID, $passWrd){
$sql = "INSERT INTO USRS (UsrID, Passwd) VALUES ('$userID','{SHA}".base64_encode(sha1($passWrd, TRUE))."')";
$result = mysql_query($sql) or die("<BR><P>Error:User Creation failed -".mysql_error()."</P>");
echo "<BR><P>User created:<B>$userID</B></P> ";
return $result;
}
// update the Users Password
function changePass ($userID, $passWrd){
$sql="UPDATE USRS set Passwd='{SHA}".base64_encode(sha1($passWrd, TRUE))."' WHERE UsrID='$userID'";
$result = mysql_query($sql) or die("<BR><P>Error:Update failed -".mysql_error()."</P>");
echo "<BR><P>Password changed for: <B>$userID</B></P>";
return $result;
}
// Add a new Group
function addGroup ($grpID){
$sql = "INSERT INTO GRPS (GRP) VALUES ('$grpID')";
$result = mysql_query($sql) or die("<BR><P>Error:Group ADD failed -$sql:".mysql_error()."</P>");
return $result;
}
// Delete a new Group
function deleteGroup ($grpID){
$sql = "DELETE FROM USRS_GRPS WHERE Grp='$grpID'";
$result = mysql_query($sql) or die("<BR><P>Error:USRS_GRPS DELETE failed -".mysql_error()."</P>");
$sql = "DELETE FROM GRPS WHERE Grp='$grpID'";
$result = mysql_query($sql) or die("<BR><P>Error:Group DELETE failed -".mysql_error()."</P>");
return $result;
}
// Add a User to a Group
function joinGroup ($userID, $grpID){
$sql = "INSERT INTO USRS_GRPS (UsrID, Grp) VALUES ('$userID','$grpID')";
$result = mysql_query($sql) or die("<BR><P>Error:Group join failed -".mysql_error()."</P>");
return $result;
}
// Remove a User from a Group
function leaveGroup ($userID, $grpID){
$sql = "DELETE FROM USRS_GRPS WHERE UsrID='$userID' AND Grp= '$grpID'";
$result = mysql_query($sql) or die("<BR><P>Error:Group exit failed -".mysql_error()."</P>");
return $result;
}
// Delete a user from the DB
function deleteUser ($userID){
$sql = " DELETE FROM USRS_GRPS where UsrID='$userID'";
$result = mysql_query($sql) or die("<BR><P>Error:USRS_GRPS Deletion failed -".mysql_error()."</P>");
$sql = "DELETE FROM USRS where UsrID='$userID'";
$result = mysql_query($sql) or die("<BR><P>Error:User Deletion failed -".mysql_error()."</P>");
return $result;
}
?>
Now let’s create a password change form, so in an editor of your choice create the file:
<HTML>
<HEAD><TITLE>Password Change Example</TITLE></HEAD>
<BODY>
<SCRIPT LANGUAGE="JavaScript">
function checkPass(theForm) {
if (theForm.usrPass.value.length == 0) {
alert("Enter a new password");
return false;
}
if (theForm.usrPass.value != theForm.chkPass.value) {
alert("Passwords differ, please try again");
return false;
}
if (theForm.usrPass.value.indexOf(" ") > -1){
alert("Passwords contains spaces, please try again");
return false;
}
return true;
}// end function checkPass
</SCRIPT>
<h4>Change your Password:</h4>
<?php function show_form ($usrPass="", $chkPass=""){ ?>
<form name="changePass" onSubmit="return checkPass(this)" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<B>User: </B><?php echo htmlspecialchars($_SERVER['REMOTE_USER'])?><br>
New Password: <input type="password" name="usrPass" VALUE="<?php echo $usrPass?>"><br>
Re-Enter New Password: <input type="password" name="chkPass" VALUE="<?php echo $chkPass?>"<br>
<input type="submit" VALUE="Change">
</form>
<?php } //End show_form ()
if (!isset($_POST['usrPass'])) {
show_form();
}
else {
if ($_POST['usrPass'] != $_POST['chkPass']) {
echo "Passwords differ, please try again";
show_form();
}
else {
// connect to the apache DB
require("/srv/www/htdocs/RegisterdUser/AdminOnly/dbConn.inc.php");
// update the Users Password
echo changePass($_SERVER['REMOTE_USER'], $_POST['usrPass']);
mysql_close();
}
}
?>
</BODY>
</HTML>
Then give it a go. Browser to
http://127.0.0.1/RegisterdUs<HTML>
<HEAD><TITLE>User Registration Example</TITLE></HEAD>
<BODY>
<SCRIPT LANGUAGE="JavaScript">
function checkFrom(theForm) {
if (theForm.usrName.value.length == 0) {
alert("Enter a new Username");
return false;
}
if (theForm.usrPass.value.length == 0) {
alert("Enter a new password");
return false;
}
if (theForm.usrPass.value != theForm.chkPass.value) {
alert("Passwords differ, please try again");
return false;
}
if (theForm.usrName.value.indexOf(" ") > -1) {
alert("UserName contains spaces, please try again");
return false;
}
if (theForm.usrPass.value.indexOf(" ") > -1){
alert("Passwords contains spaces, please try again");
return false;
}
return true;
}// end function checkPass
</SCRIPT>
<h4>Create a User-ID:</h4>
<?php function show_form ($usrName="", $usrPass="", $chkPass=""){ ?>
<form name="regUser" onSubmit="return checkFrom(this)" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
User Name: <input type="text" name="usrName" VALUE="<?php echo $usrName?>"><br>
New Password: <input type="password" name="usrPass" VALUE="<?php echo $usrPass?>"><br>
Re-Enter New Password: <input type="password" name="chkPass" VALUE="<?php echo $chkPass?>"><br>
<input type="submit" VALUE="Submit">
</form>
<?php } //End show_form ()
if (!isset($_POST['usrName'])) {
show_form();
}
else {
if ($_POST['usrPass'] != $_POST['chkPass']) {
echo "Passwords differ, please try again";
show_form();
}
else {
// connect to the apache DB
require("/srv/www/htdocs/RegisterdUser/AdminOnly/dbConn.inc.php");
// Insert the New User into the DB
echo addUser($_POST['usrName'], $_POST['usrPass']);
mysql_close();
}
}
?>
</BODY>
</HTML>
Give it a go. Browse to
http://127.0.0.1/regUser.php<HTML>
<HEAD><TITLE>User Maintenance</TITLE></HEAD>
<BODY>
<H1>Registered Users:</H1>
<BR>
<?php function show_form (){
// Query the Registered users and their groups
$query="SELECT USRS.UsrID, Grp FROM USRS LEFT JOIN USRS_GRPS ON USRS.UsrID=USRS_GRPS.UsrID";
$result=mysql_query($query) or die(mysql_error());
// Print out resulta
echo '<form name="maintUsr" action="'.$_SERVER['PHP_SELF'].'" method="post">';
echo "\n<TABLE>\n";
for($i = 0; $i < mysql_num_fields($result); $i++){
echo "<th>".htmlspecialchars(mysql_field_name($result, $i))."</th>";
}
echo "<th>Delete</th></tr>\n";
$rows=0;
while($row = mysql_fetch_array($result)){
echo "<tr>";
for($i = 0; $i < mysql_num_fields($result); $i++){
echo "<td>".htmlspecialchars($row[$i])."</td>";
}
$rows++;
echo "<td><input type=\"checkbox\" name=\"delUsr[]\" value=\"$row[0]\" ></td></tr>\n";
}
echo "</TABLE>\n<BR>\n<input type=\"submit\" VALUE=\"Delete\">\n";
echo "</form>\n<BR>\n<HR>\n";
echo "<P><B>Your: </B>".$_SERVER['REMOTE_USER']."</p>";
} //End show_form ()
// connect to the apache DB
require("/srv/www/htdocs/RegisterdUser/AdminOnly/dbConn.inc.php");
// Get the array of users to delete
$delList=$_POST['delUsr'];
if (empty($delList)) {
show_form();
}
else {
$toDel = count($delList);
for($i=0; $i < $toDel; $i++) {
// Delete the user from the DB
echo deleteUser($delList[$i]);
}
}
mysql_close();
?>
</BODY>
</HTML>
Give it a go. Browse to:
http://127.0.0.1/RegisterdU<HTML>
<HEAD><TITLE>Group Maintenance Example</TITLE></HEAD>
<BODY>
<h4>Group Maintenance</h4>
<u>Existing Groups:</u>
<?php function show_form ($grpID=""){
// Query the Registered users and their groups
$query="SELECT Grp FROM GRPS";
$result=mysql_query($query) or die(mysql_error());
// Print out the results
echo '<form name="maintGrp" action="'.$_SERVER['PHP_SELF'].'" method="post">';
echo "\n<TABLE>\n";
for($i = 0; $i < mysql_num_fields($result); $i++){
echo "<th>".htmlspecialchars(mysql_field_name($result, $i))."</th>";
}
echo "<th>Delete</th></tr>\n";
$rows=0;
while($row = mysql_fetch_array($result)){
echo "<tr>";
for($i = 0; $i < mysql_num_fields($result); $i++){
echo "<td>".htmlspecialchars($row[$i])."</td>";
}
$rows++;
echo "<td><input type='checkbox' name='delGrp[]' value='$row[0]' ></td></tr>\n";
}
echo "</TABLE>\n<BR>\nGroup Name: <input type=\"text\" name=\"grpID\" VALUE=\"$grpID\"><input type=\"submit\" VALUE=\"Submit\">\n";
echo "</form>\n<BR>\n<HR>\n";
echo "<P><B>Your: </B>".$_SERVER['REMOTE_USER']."</p>";
} //End show_form ()
require("/srv/www/htdocs/RegisterdUser/AdminOnly/dbConn.inc.php");
// Get the array of Groups to delete and any new Group name
$delList=$_POST['delGrp'];
$newGrp=$_POST['grpID'];
if ( ($newGrp == "") && empty($delList)) {
show_form();
}
else {
if ($newGrp != ""){
echo addGroup($newGrp);
}
if (!empty($delList)) {
$toDel = count($delList);
for($i=0; $i < $toDel; $i++) {
// Delete the user from the DB
echo deleteGroup($delList[$i]);
}
}
}
mysql_close();
?>
</BODY>
</HTML>
Give it a go. Browse to:
http://127.0.0.1/RegisterdUs<HTML>
<HEAD><TITLE>Group Membership Maintenance</TITLE></HEAD>
<BODY>
<H1>Membership:</H1>
<BR>
<?php function show_form (){
// Build the Group Option lists
$query="SELECT Grp FROM GRPS";
$result=mysql_query($query) or die(mysql_error());
$optionList=array();
while($row = mysql_fetch_array($result)){
$optionList[]=$row[0];
}
// Query the Registered users and their groups
$query="SELECT USRS.UsrID, Grp FROM USRS LEFT JOIN USRS_GRPS ON USRS.UsrID=USRS_GRPS.UsrID";
$result=mysql_query($query) or die(mysql_error());
// Print out the results
echo '<form name="maintUsr" action="'.$_SERVER['PHP_SELF'].'" method="post">';
echo "\n<TABLE>\n";
for($i = 0; $i < mysql_num_fields($result); $i++){
echo "<th>".htmlspecialchars(mysql_field_name($result, $i))."</th>";
}
echo "<th>Delete Membership</th><th>Join Group</th></tr>\n";
$lastUsr="";
while($row = mysql_fetch_array($result)){
if ($lastUsr == $row[0]){
$theUSR="";
}
else {
$theUSR=htmlspecialchars($row[0]);
}
echo "<tr><td>$theUSR</td><td>".htmlspecialchars($row[1])."</td>";
$lastUsr="$row[0]";
$delBox="";
if ($row[1] != ""){
$delBox="<input type=\"checkbox\" name=\"delUsrGRP[]\" value=\"$row[1] $row[0]\" >";
}
echo "<td>$delBox</td><td><select name=\"joinGRP[]\">\n";
reset($optionList);
while(list($ind,$grp)=each($optionList)){
echo " <option value=\"$row[0] $grp\">$grp</option>\n";
}
echo "</select></tr>\n";
}
echo "</TABLE>\n<BR>\n<input type=\"submit\" VALUE=\"Update\">\n";
echo "</form>\n<BR>\n<HR>\n";
echo "<P><B>Your: </B>".$_SERVER['REMOTE_USER']."</p>";
} //End show_form ()
// connect to the apache DB
require("/srv/www/htdocs/RegisterdUser/AdminOnly/dbConn.inc.php");
// Get the array of users to delete
$delList=$_POST['delUsrGRP'];
$joinList=array();
while(list($ind,$selection)=each($_POST['joinGRP'])){
$toJoin=explode(" ",$selection);
if ($toJoin[1] != "") {
$joinList[]=$selection;
}
}
if (empty($delList) && empty($joinList)) {
show_form();
}
else {
//Delete the marked Associations
$toDel = count($delList);
for($i=0; $i < $toDel; $i++) {
$mbrs=explode(" ",$delList[$i]);
// Delete the user from the DB
echo leaveGroup($mbrs[1], $mbrs[0]);
}
//Add the Selected Associations
$toJoin = count($joinList);
for($i=0; $i < $toJoin; $i++) {
$mbrs=explode(" ",$joinList[$i]);
// Delete the user from the DB
echo joinGroup($mbrs[0], $mbrs[1]);
}
}
mysql_close();
?>
</BODY>
</HTML>
Give it a go. Browse to:
http://127.0.0.1/RegisterdU<HTML><BODY>
<H3>Apache DBD MySQL Admin pages</H3>
<ul>
<li><A HREF="maintUSRS.php">User Maintenance</A></li>
<li><A HREF="maintGRPS.php">Group Maintenance</A></li>
<li><A HREF="maintMBRS.php">User / Group Association</A></li>
<li><A HREF="../chngPass.php">Change your password</A></li>
<li><A HREF="../../regUser.php">Register a New user</A></li>
</ul>
</BODY></HTML>
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)