ssh_controls/convert_pubkey.pl
Patrick Van der Veken 8528787e97 Initial commit
2015-05-19 11:34:18 +02:00

201 lines
5.3 KiB
Perl

#!/bin/env perl
#******************************************************************************
# @(#) convert_pubkey.pl
#******************************************************************************
# @(#) Copyright (C) 2014 by KUDOS BVBA <info@kudos.be>. All rights reserved.
#
# This program is a free software; you can redistribute it and/or modify
# it under the same terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details
#******************************************************************************
#******************************************************************************
# PRAGMAs/LIBs
#******************************************************************************
use strict;
use Getopt::Long;
use Pod::Usage;
#******************************************************************************
# DATA structures
#******************************************************************************
# ------------------------- CONFIGURATION starts here -------------------------
# define the V.R.F (version/release/fix)
my $MY_VRF = "1.0.0";
# always assume RSA keys!
my $key_algo = 'ssh-rsa';
# ------------------------- CONFIGURATION ends here ---------------------------
# initialize variables
my (%options, @key_file);
my ($key_file, $key_label, $key_line, $key_type);
$|++;
#******************************************************************************
# MAIN routine
#******************************************************************************
# -----------------------------------------------------------------------------
# process script arguments & options
# -----------------------------------------------------------------------------
unless (@ARGV > 0) { pod2usage(-verbose => 0) };
Getopt::Long::Configure ('prefix_pattern=(--|-|\/)', 'bundling', 'no_ignore_case');
GetOptions (\%options,
qw(
file|f=s
help|h|?
label|l=s
version|V
));
pod2usage(-verbose => 0) unless (%options);
# check version parameter
if ($options{'version'}) {
print "INFO: $0: version $MY_VRF";
exit (0);
}
# check help parameter
if ($options{'help'}) {
pod2usage(-verbose => 3);
exit (0);
};
# check file parameter
if ($options{'file'}) {
$key_file = $options{'file'};
unless (-f $key_file) {
die "ERROR: input file $key_file not found [$!]";
}
} else {
die "ERROR: missing value for --file parameter";
}
# check label parameter
if ($options{'label'}) {
$key_label = $options{'label'};
if (not (defined ($key_label) or $key_label eq "")) {
die "ERROR: key label not defined";
}
} else {
die "ERROR: missing value for --label parameter";
}
# -----------------------------------------------------------------------------
# read key file contents & process key
# -----------------------------------------------------------------------------
open (KEY_FILE, $key_file) or die "ERROR: unable to read file";
chomp (@key_file = <KEY_FILE>);
close (KEY_FILE);
# determine type of key
if (grep { /$key_algo/ } @key_file) {
$key_type = 1;
} else {
$key_type = 2;
}
# process key
SWITCH: {
$key_type == 1 && do {
# 1 line only!!
foreach (@key_file) {
chomp;
($key_algo, $key_line) = split (/ /);
};
last SWITCH;
};
$key_type == 2 && do {
foreach (@key_file) {
chomp;
# skip BEGIN/END lines
next if (/^----/);
# skip Comment field
next if (/^Comment/);
$key_line .= $_;
}
last SWITCH;
};
}
# mangle space in label
$key_label =~ tr/ /_/s;
# print result
print "$key_label,$key_algo,$key_line\n";
exit (0);
#******************************************************************************
# End of SCRIPT
#******************************************************************************
#******************************************************************************
# POD
#******************************************************************************
# -----------------------------------------------------------------------------
=head1 NAME
convert_pubkey.pl - converts public keys for SSH controls format (SSH-RSA)
=head1 SYNOPSIS
update_ssh.pl -f|--file=<public_key> -l|--label=<label_name>
[-h|--help]
[-V|--version]
=head1 DESCRIPTION
B<convert_pubkey.pl> converts public keys generated by PuTTYgen or ssh-keygen into the
correct format for the SSH controls key store.
=head1 OPTIONS
=over 2
=item -h | --help
S< >Show the help page.
=item -f | --file
S< >File containing the public key to be converted.
=item -l | --label
S< >Name for the public key in SSH control. This name will be used as identifier of the key in the different SSH controls configuration files.
=item -V | --version
S< >Show version of the script.
=back
=head1 NOTES
=over 2
=item * Options may be preceded by a - (dash), -- (double dash) or a / (slash).
=back
=head1 AUTHOR
(c) KUDOS BVBA, Patrick Van der Veken
=head1 HISTORY
@(#) 2014-12-20: VRF 1.0.0: first version [Patrick Van der Veken]