1
0
mirror of https://github.com/Yubico/yubikey-ksm.git synced 2024-12-11 09:24:14 +01:00
yubikey-ksm/ykksm-export

118 lines
3.8 KiB
Plaintext
Raw Normal View History

2009-04-19 07:39:30 +02:00
#!/usr/bin/perl
# Written by Simon Josefsson <simon@josefsson.org>.
2012-01-23 20:47:28 +01:00
# Copyright (c) 2009-2012 Yubico AB
2009-04-19 07:39:30 +02:00
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided
# with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
use strict;
use DBI;
use MIME::Base64;
sub usage {
print "Usage: $0 [--verbose] [--help] [--database DBI] [--db-user USER] [--db-passwd PASSWD]\n";
2009-04-19 07:39:30 +02:00
print "\n";
print "Tool to export keys to the YKKSM-KEYPROV format.\n";
2009-04-19 07:39:30 +02:00
print "\n";
print " --database DBI: Database identifier, see http://dbi.perl.org/\n";
print " defaults to a MySQL database ykksm on localhost,\n";
print " i.e., DBI::mysql:ykksm.\n";
print "\n";
print " --db-user USER: Database username to use, defaults to empty string.\n";
print "\n";
print " --db-passwd PASSWD: Database password to use, defaults to empty string.\n";
print "\n";
print "Usage example:\n";
print "\n";
print " ./ykksm-export --db-user foo --db-passwd pass123 |\n";
2009-04-19 08:05:46 +02:00
print " gpg -a --sign --encrypt -r 1D2F473E > keys.txt\n";
2009-04-19 07:39:30 +02:00
print "\n";
exit 1;
}
#Support dbconfig-common generated database settings, if available
our ($dbuser, $dbpass, $basepath, $dbname, $dbserver, $dbport, $dbtype);
if( -r '/etc/yubico/ksm/config-db.cfg' ) {
require '/etc/yubico/ksm/config-db.cfg';
} else {
$dbtype = 'mysql';
$dbname = 'ykksm';
}
2009-04-19 07:39:30 +02:00
my $verbose = 0;
my $db = "dbi:$dbtype:$dbname";
2009-04-19 07:39:30 +02:00
while ($ARGV[0] =~ m/^-(.*)/) {
my $cmd = shift @ARGV;
if (($cmd eq "-v") || ($cmd eq "--verbose")) {
$verbose = 1;
} elsif (($cmd eq "-h") || ($cmd eq "--help")) {
usage();
} elsif ($cmd eq "--database") {
$db = shift;
} elsif ($cmd eq "--db-user") {
$dbuser = shift;
} elsif ($cmd eq "--db-passwd") {
$dbpass = shift;
2009-04-19 07:39:30 +02:00
}
}
if ($#ARGV>=0) {
usage();
}
my $dbh = DBI->connect($db, $dbuser, $dbpass, {'RaiseError' => 1});
2009-04-19 07:39:30 +02:00
my $sth = $dbh->prepare
2009-12-14 17:18:38 +01:00
('SELECT serialnr, publicname, internalname, aeskey, lockcode, created FROM yubikeys')
2009-04-19 07:39:30 +02:00
or die "Couldn't prepare statement: " . $dbh->errstr;
$sth->execute()
or die "Couldn't execute statement: " . $sth->errstr;
my $row;
while ($row = $sth->fetchrow_hashref()) {
if ($verbose) {
print "# ";
foreach my $key (keys %$row) {
print $key . "=" . $row->{$key} . " ";
}
print "\n";
}
2009-12-14 17:18:38 +01:00
print $row->{'serialnr'} . ",";
print $row->{'publicname'} . ",";
print $row->{'internalname'} . ",";
print $row->{'aeskey'} . ",";
print $row->{'lockcode'} . ",";
2009-04-19 07:59:44 +02:00
print $row->{'created'} . ",\n";
2009-04-19 07:39:30 +02:00
}
if ($sth->rows == 0) {
print "No data?!\n\n";
}
$sth->finish;
$dbh->disconnect();
exit 0;