summaryrefslogtreecommitdiff
path: root/lib/rddb.pm
blob: c12ea2606aefbfd0e986ba143362fe03f64de0f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/perl

use strict;
use Config::IniFiles;
use DBI;

package rddb;


sub opendb
{
  my $RD_CONF = "/etc/rd.conf";
  my $cfg = Config::IniFiles->new(-file => $RD_CONF)
    or return (undef , 'ERROR', "Config File Error: " . join("\n", @Config::IniFiles::errors));

  my $dbhost = $cfg->val('mySQL', 'Hostname');
  my $dbname = $cfg->val('mySQL', 'Database');
  my $dbuser = $cfg->val('mySQL', 'Loginname');
  my $dbpasswd = $cfg->val('mySQL', 'Password');

  my $dbh = DBI->connect("DBI:mysql:$dbname:$dbhost","$dbuser","$dbpasswd")
    or return (undef, 'ERROR', "Database Error: " . $DBI::errstr);

  return ($dbh, 'OK', 'success');
}

sub closedb
{
  my $dbh = shift;
  $dbh->disconnect();
}

sub get_token
{
  my ($dbh, $username) = @_;

  my $sth = $dbh->prepare('select PASSWORD from USERS where LOGIN_NAME = ?')
    or return ('', 'ERROR', "Database Error: " . $dbh->errstr);

  $sth->execute($username)
    or return ('', 'ERROR', "Database Error: " . $sth->errstr);

  my ($token) = $sth->fetchrow_array;
  $sth->finish();

  unless(defined $token) {
    return ('', 'ERROR', "user '" . $username . "' not known by rivendell")
  }
  return ($token, 'OK', 'success');
}

sub check_token
{
  my ($dbh, $username, $token) = @_;

  my $sth = $dbh->prepare('select PASSWORD from USERS where LOGIN_NAME = ?')
    or return (0, 'ERROR', "Database Error: " . $dbh->errstr);

  $sth->execute($username)
    or return (0, 'ERROR', "Database Error: " . $sth->errstr);

  my ($token_result) = $sth->fetchrow_array;
  $sth->finish();

  unless(defined $token_result) {
    return (0, 'ERROR', "user '" . $username . "' not known by rivendell")
  }
  
  if($token_result == $token) {
    return (1, 'OK', 'success');
  }
  return (0, 'ERROR', "wrong password");
}

return 1;