diff options
author | Christian Pointner <equinox@helsinki.at> | 2014-09-18 14:14:35 (GMT) |
---|---|---|
committer | Christian Pointner <equinox@helsinki.at> | 2014-09-18 14:14:35 (GMT) |
commit | 21d59b1d774f4bba42b36e927b2069fe46cdd6f9 (patch) | |
tree | b37b82cd1fb99bb29927c193eb91f4c848f0d577 /lib | |
parent | 2783f60fe9d8937fa3f4ae52527441a6751d6427 (diff) |
moved rivendel database code to own module
Diffstat (limited to 'lib')
-rwxr-xr-x | lib/rddb.pm | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/lib/rddb.pm b/lib/rddb.pm new file mode 100755 index 0000000..1ef296c --- /dev/null +++ b/lib/rddb.pm @@ -0,0 +1,48 @@ +#!/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 get_token +{ + my $username = shift; + + my ($dbh, $state, $errorstring) = opendb(); + unless($dbh) { + return ('', $state, $errorstring); + } + + 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; + return ($token, 'OK', 'success') if(defined $token); + + return ('', 'ERROR', "user '" . $username . "' not known by rivendell") +} + +return 1; |