| 1 | package DJabberd::Authen::DBI; | ||
| 2 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use base 'DJabberd::Authen'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 1 | ▶ | use Digest::MD5 qw(md5_hex md5_base64); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use Digest::SHA1 qw(sha1_hex sha1_base64); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use DJabberd::Log; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | our $logger = DJabberd::Log->get_logger; | ||
| 10 | 1 | ▶ | use DBI; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | our $VERSION = '0.2'; | ||
| 13 | |||
| 14 | 0 | ▶ | sub log { |
|
Subroutine
log: not called CC=1 SCAR=6.9
| |||
| 15 | 0 | $logger; | |
| 16 | } | ||
| 17 | |||
| 18 | # configuration loading | ||
| 19 | BEGIN { | ||
| 20 | 1 | ▶ | no strict 'refs'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 21 | for my $name qw(dsn user pass query args) { | ||
| 22 | *{"set_config_".$name} = sub { | ||
| 23 | my $self = shift; | ||
| 24 | $self->{$name} = shift if @_; | ||
| 25 | $self->{$name}; | ||
| 26 | } | ||
| 27 | } | ||
| 28 | } | ||
| 29 | |||
| 30 | sub blocking { 1; } | ||
| 31 | sub can_register_jids { 0; } | ||
| 32 | sub can_unregister_jids { 0; } | ||
| 33 | sub can_retrieve_cleartext { 0; } | ||
| 34 | |||
| 35 | # finalize configuration | ||
| 36 | sub finalize { | ||
| 37 | my $self = shift; | ||
| 38 | |||
| 39 | die 'No database configured.' unless $self->{dsn}; | ||
| 40 | die 'No query specified.' unless $self->{query}; | ||
| 41 | |||
| 42 | $self->{dbi} = DBI->connect(map { $self->{$_} } qw(dsn user pass)) | ||
| 43 | or die 'Error connectin to the database. '.$DBI::errstr; | ||
| 44 | $logger->debug('Connected to database '.$self->{dsn}); | ||
| 45 | |||
| 46 | $self->{sth} = $self->{dbi}->prepare($self->{query}) | ||
| 47 | or die 'Error preparing statement. '.$self->{dbi}->errstr; | ||
| 48 | $logger->debug('Query prepared '.$self->{dsn}); | ||
| 49 | |||
| 50 | } | ||
| 51 | |||
| 52 | sub check_cleartext { | ||
| 53 | my ($self, $cb, %args) = @_; | ||
| 54 | my $username = $args{username}; | ||
| 55 | my $password = $args{password}; | ||
| 56 | my $conn = $args{conn}; | ||
| 57 | unless ($username =~ /^\w+$/) { | ||
| 58 | $cb->reject; | ||
| 59 | return; | ||
| 60 | } | ||
| 61 | |||
| 62 | my %arguments = | ||
| 63 | ( login => $username, | ||
| 64 | password => $password, | ||
| 65 | password_sha1_hex => sha1_hex($password), | ||
| 66 | password_sha1_base64 => sha1_base64($password), | ||
| 67 | password_md5_hex => md5_hex($password), | ||
| 68 | password_md5_base64 => md5_base64($password) ); | ||
| 69 | |||
| 70 | $self->{sth}->execute | ||
| 71 | ( map { $arguments{$_} } split /,/, $self->{args} ) | ||
| 72 | or do { | ||
| 73 | $logger->debug('Error Executing query '.$self->{dbi}->errstr); | ||
| 74 | $cb->reject(); | ||
| 75 | return | ||
| 76 | }; | ||
| 77 | |||
| 78 | if (my ($data) = $self->{sth}->fetchrow_array) { | ||
| 79 | $logger->debug('User '.$username.' authenticated'); | ||
| 80 | $cb->accept(); | ||
| 81 | } else { | ||
| 82 | $logger->debug('Failed authentication for user '.$username); | ||
| 83 | $cb->reject(); | ||
| 84 | } | ||
| 85 | |||
| 86 | } | ||
| 87 | |||
| 88 | 1; | ||
| 89 | |||
| 90 | __END__ | ||