##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
-## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
##
use DBI;
# c2ph says: typedef='s2 l2 i', sizeof=16
my $FLOCK_STRUCT = 's2l2i';
-sub setlock ($;$$)
+sub setlock($;$$)
{
my ($fh, $start, $len) = @_;
$start = 0 unless defined $start;
sub usage()
{
print STDERR <<HERE;
-usage: radsqlrelay [-d sql_driver] [-b database] [-h host] [-u user] [-p password] [-1] file_path
+usage: radsqlrelay [options] file_path
options:
-? Print this help message.
-1 One-shot mode: push the file to database and exit.
-b database Name of the database to use.
- -d sql_driver Driver to use: mysql, pg.
+ -d sql_driver Driver to use: mysql, pg, oracle.
+ -f file Read password from file, instead of command line.
-h host Connect to host.
+ -P port Port number to use for connection.
-p passord Password to use when connecting to server.
-u user User for login.
-x Turn on debugging.
{
my ($dbinfo, $path) = @_;
- until (rename($path, $path.'.work')) {
- if ($! == ENOENT) {
- sleep(1);
- return if $need_exit;
- } else {
- print STDERR "error: Couldn't move $path to $path.work: $!\n";
- exit 1;
+ unless (-e $path.'.work') {
+ until (rename($path, $path.'.work')) {
+ if ($! == ENOENT) {
+ sleep(1);
+ return if $need_exit;
+ } else {
+ print STDERR "error: Couldn't move $path to $path.work: $!\n";
+ exit 1;
+ }
}
}
}
unlink($path.'.work');
- close(FILE); # and unlock #
+ close(FILE); # and unlock
}
# sub main()
my %args = (
- d => 'mysql',
b => 'radius',
+ d => 'mysql',
h => 'localhost',
- u => 'radius',
p => 'radius',
+ u => 'radius',
);
-my $ret = getopts("d:b:fh:u:p:x1?", \%args);
+my $ret = getopts("b:d:f:h:P:p:u:x1?", \%args);
if (!$ret or @ARGV != 1) {
usage();
exit 1;
}
my $data_source;
-if ($args{d} eq 'mysql') {
+if (lc($args{d}) eq 'mysql') {
$data_source = "DBI:mysql:database=$args{b};host=$args{h}";
-} elsif ($args{d} eq 'pg') {
+} elsif (lc($args{d}) eq 'pg') {
$data_source = "DBI:Pg:dbname=$args{b};host=$args{h}";
+} elsif (lc($args{d}) eq 'oracle') {
+ $data_source = "DBI:Oracle:$args{b}";
} else {
print STDERR "error: SQL driver not supported yet: $args{d}\n";
exit 1;
}
+$data_source .= ";port=$args{P}" if $args{'P'};
+
+my $pw;
+if($args{f}) {
+ open(FILE, "< $args{f}") or die "error: Couldn't open $args{f}: $!\n";
+ $pw = <FILE>;
+ chomp($pw);
+ close(FILE);
+} else {
+ # args{p} is always defined.
+ $pw = $args{p};
+}
$SIG{INT} = \&got_signal;
$SIG{TERM} = \&got_signal;
my %dbinfo = (
base => $data_source,
user => $args{u},
- pass => $args{p},
+ pass => $pw,
);
connect_wait(\%dbinfo);
my $path = shift @ARGV;
-if (-e $path.'.work') {
- process_file(\%dbinfo, $path.'.work');
-}
-
until ($need_exit) {
process_file(\%dbinfo, $path);
last if ($args{1} || $need_exit);