From 21d59b1d774f4bba42b36e927b2069fe46cdd6f9 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Thu, 18 Sep 2014 14:14:35 +0000 Subject: moved rivendel database code to own module diff --git a/authtoken.json b/authtoken.json index ef279c4..dbdc64a 100755 --- a/authtoken.json +++ b/authtoken.json @@ -1,44 +1,16 @@ #!/usr/bin/perl use strict; -use warnings; -use Config::IniFiles; -use DBI; - -sub get_token -{ - my $username = shift; - - my $RD_CONF = "/etc/rd.conf"; - my $cfg = Config::IniFiles->new(-file => $RD_CONF) - or return ('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 ('ERROR', "Database Error: " . $DBI::errstr, ''); - - 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 ('OK', 'success', $token) if(defined $token); - - return ('ERROR', "user '" . $username . "' not known by rivendell", '') -} +use File::Basename; +use lib dirname( __FILE__ ) . '/lib'; +use rddb; my $status = 'ERROR'; my $errorstring = 'unknown'; my $username = ''; my $token = ''; if(defined $ENV{REMOTE_USER}) { - ($status, $errorstring, $token) = get_token($ENV{REMOTE_USER}); + ($token, $status, $errorstring) = rddb::get_token($ENV{REMOTE_USER}); $username = $ENV{REMOTE_USER}; } else { $errorstring = 'no username defined - are you logged in?'; 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; -- cgit v0.10.2