| 1 | package VUser::Auth::SQLite; | ||
| 2 | use warnings; | ||
| 3 | use strict; | ||
| 4 | |||
| 5 | # Copyright 2005 Randy Smith | ||
| 6 | # $Id: SQLite.pm,v 1.5 2006-01-04 21:57:48 perlstalker Exp $ | ||
| 7 | |||
| 8 | our $REVISION = (split (' ', '$Revision: 1.5 $'))[1]; | ||
| 9 | our $VERSION = "0.3.0"; | ||
| 10 | |||
| 11 | sub revision { return $REVISION; } | ||
| 12 | sub version { return $VERSION; } | ||
| 13 | |||
| 14 | use DBI; | ||
| 15 | use VUser::ACL qw/:consts/; | ||
| 16 | |||
| 17 | my $dbh = undef; | ||
| 18 | my $table = 'auth'; | ||
| 19 | |||
| 20 | sub init | ||
| 21 | { | ||
| 22 | my $acl = shift; | ||
| 23 | my $cfg = shift; | ||
| 24 | |||
| 25 | $acl->register_auth(\&auth_user); | ||
| 26 | |||
| 27 | my $db = VUser::ExtLib::strip_ws($cfg->{'ACL SQLite'}{'file'}); | ||
| 28 | if (not -e $db) { | ||
| 29 | $dbh = DBI->connect("dbi:SQLite:dbname=$db", '', ''); | ||
| 30 | # Database didn't exist so we need to create it. | ||
| 31 | my $sql = "create table $table ("; | ||
| 32 | $sql .= " user varchar(128) not null primary key"; | ||
| 33 | $sql .= ", ip varchar(20)"; | ||
| 34 | $sql .= ", password varchar(40) not null"; | ||
| 35 | $sql .= ")"; | ||
| 36 | $dbh->do($sql) or die "DB Error: ".$dbh->errstr; | ||
| 37 | } else { | ||
| 38 | $dbh = DBI->connect("dbi:SQLite:dbname=$db", '', ''); | ||
| 39 | } | ||
| 40 | } | ||
| 41 | |||
| 42 | sub auth_add | ||
| 43 | { | ||
| 44 | my ($cfg, $opts, $action, $eh) = @_; | ||
| 45 | |||
| 46 | my $sql = "insert into $table "; | ||
| 47 | $sql .= " values (?, ?, ?)"; | ||
| 48 | |||
| 49 | my $sth = $dbh->prepare($sql) or die "DB Error: ".$dbh->errstr; | ||
| 50 | $sth->execute($opts->{'user'}, | ||
| 51 | defined $opts->{'ip'} ? $opts->{'ip'} : '', | ||
| 52 | $opts->{'password'}) | ||
| 53 | or die "DB Error: ".$sth->errstr; | ||
| 54 | } | ||
| 55 | |||
| 56 | sub auth_del | ||
| 57 | { | ||
| 58 | my ($cfg, $opts, $action, $eh) = @_; | ||
| 59 | |||
| 60 | my $sql = "delete from $table where user = ?"; | ||
| 61 | my $sth = $dbh->prepare($sql) or die "DB Error: ".$dbh->errstr; | ||
| 62 | $sth->execute($opts->{'user'}) | ||
| 63 | or die "DB Error: ".$sth->errstr; | ||
| 64 | } | ||
| 65 | |||
| 66 | sub auth_user | ||
| 67 | { | ||
| 68 | my ($cfg, $user, $pass, $ip) = @_; | ||
| 69 | |||
| 70 | my $sql = "select * from $table where user = ?"; | ||
| 71 | my $sth = $dbh->prepare($sql) or die "DB Error: ".$dbh->errstr; | ||
| 72 | $sth->execute($user) or die "DB Error: ".$dbh->errstr; | ||
| 73 | |||
| 74 | my $res = $sth->fetchrow_hashref; | ||
| 75 | if (not defined $res) { | ||
| 76 | return UNKNOWN(); | ||
| 77 | } elsif ($pass eq $res->{password}) { | ||
| 78 | return ALLOW(); | ||
| 79 | } else { | ||
| 80 | return DENY(); | ||
| 81 | } | ||
| 82 | |||
| 83 | } | ||
| 84 | |||
| 85 | sub auth_get | ||
| 86 | { | ||
| 87 | my ($cfg, $opts, $action, $eh) = @_; | ||
| 88 | |||
| 89 | my $user = $opts->{user} || '%'; | ||
| 90 | |||
| 91 | my $sql = "select * from $table where user like ?"; | ||
| 92 | my $sth = $dbh->prepare($sql) or die 'DB Error: '.$dbh->errstr; | ||
| 93 | $sth->execute($user) or die 'DB Error: '.$dbh->errstr; | ||
| 94 | |||
| 95 | my @users = (); | ||
| 96 | |||
| 97 | my $res; | ||
| 98 | while (defined ($res = $sth->fetchrow_hashref)) { | ||
| 99 | push @users, {user => $res->{user}, | ||
| 100 | password => $res->{password}, | ||
| 101 | ip => $res->{ip}}; | ||
| 102 | } | ||
| 103 | |||
| 104 | return @users; | ||
| 105 | } | ||
| 106 | |||
| 107 | 1; | ||
| 108 | |||
| 109 | __END__ |