MySQL module
Karjala
karjala_lists at karjala.org
Mon Jul 3 07:56:29 UTC 2006
Instructions are in the POD. I tested it, it works.
Brad Fitzpatrick wrote:
> You going to write the MySQL authen plugin?
>
-------------- next part --------------
package DJabberd::Authen::MySQL;
use strict;
use base 'DJabberd::Authen';
use DJabberd::Log;
our $logger = DJabberd::Log->get_logger;
use DBI;
sub log {
$logger;
}
=head1 NAME
DJabberd::Authen::MySQL - A MySQL authentication module for DJabberd
=cut
=head1 SYNOPSIS
<VHost mydomain.com>
[...]
<Plugin DJabberd::Authen::MySQL>
DBName mydbname
DBHost 192.168.12.35 # optional
DBPort 6723 # optional
DBUserName adbuser
DBPassword somepass
DBTable djusers
DBUsernameColumn djusername
DBPasswordColumn djpassword
DBEncryptedPassword 0 # optional
DBWhere canjabber = 1 # optional
</Plugin>
</VHost>
=cut
sub set_config_dbname {
my ($self, $dbname) = @_;
$self->{'mysql_dbname'} = $dbname;
}
sub set_config_dbusername {
my ($self, $dbusername) = @_;
$self->{'mysql_dbusername'} = $dbusername;
}
sub set_config_dbpassword {
my ($self, $dbpassword) = @_;
$self->{'mysql_dbpassword'} = $dbpassword;
}
sub set_config_dbhost {
my ($self, $dbhost) = @_;
$self->{'mysql_dbhost'} = $dbhost;
}
sub set_config_dbport {
my ($self, $dbport) = @_;
$self->{'mysql_dbport'} = $dbport;
}
sub set_config_dbtable {
my ($self, $dbtable) = @_;
$self->{'mysql_table'} = $dbtable;
}
sub set_config_dbusernamecolumn {
my ($self, $dbusernamecolumn) = @_;
$self->{'mysql_usernamecolumn'} = $dbusernamecolumn;
}
sub set_config_dbpasswordcolumn {
my ($self, $dbpasswordcolumn) = @_;
$self->{'mysql_passwordcolumn'} = $dbpasswordcolumn;
}
sub set_config_dbencryptedpassword {
my ($self, $dbencryptedpassword) = @_;
$self->{'mysql_encryptedpassword'} = $dbencryptedpassword;
}
sub set_config_dbwhere {
my ($self, $dbwhere) = @_;
$self->{'mysql_where'} = $dbwhere;
}
sub finalize {
my $self = shift;
my $dsn = "DBI:mysql:database=$self->{'mysql_dbname'}";
if (defined $self->{'mysql_dbhost'}) { $dsn .= ";host=$self->{'mysql_dbhost'}"; }
if (defined $self->{'mysql_dbport'}) { $dsn .= ";port=$self->{'mysql_dbport'}"; }
my $dbh = DBI->connect($dsn, $self->{'mysql_dbusername'}, $self->{'mysql_dbpassword'}, { RaiseError => 1 });
$self->{'mysql_dbh'} = $dbh;
}
sub can_retrieve_cleartext { 0 }
sub check_cleartext {
my ($self, $cb, %args) = @_;
my $username = $args{username};
my $password = $args{password};
my $conn = $args{conn};
unless ($username =~ /^\w+$/) {
$cb->reject;
return;
}
my $dbh = $self->{'mysql_dbh'};
my $sql_username = "select $self->{'mysql_usernamecolumn'} from $self->{'mysql_table'} where $self->{'mysql_usernamecolumn'} = ".$dbh->quote($username);
my $sql_password = " and $self->{'mysql_passwordcolumn'} = ".($self->{'mysql_encryptedpassword'} ? "PASSWORD(".$dbh->quote($password).")" : $dbh->quote($password));
my $sql_where = (defined $self->{'mysql_where'} ? " and $self->{'mysql_where'}" : "");
if (defined(($dbh->selectrow_array("$sql_username $sql_password $sql_where"))[0])) {
$cb->accept;
$logger->debug("User '$username' successfully logged in");
return 1;
} else {
$cb->reject();
if (defined(($dbh->selectrow_array("$sql_username $sql_where"))[0])) { # if user exists
$logger->info("User '$username' denied, password error");
return 0;
} else {
$logger->info("User '$username' denied, does not exist in database");
return 1;
}
}
}
1;
More information about the Djabberd
mailing list