Skip to content
Snippets Groups Projects
Commit 949d2644 authored by Nigel Kukard's avatar Nigel Kukard
Browse files

Added username specification support

parent 0ec0b2e7
No related branches found
No related tags found
No related merge requests found
......@@ -45,7 +45,7 @@ if (!eval {require Term::ReadKey; 1;}) {
use User::pwent;
my $VERSION = "0.1.1";
my $VERSION = "0.1.3";
print(STDERR "AWIT-SSH-Client v$VERSION - Copyright (c) 2016, AllWorldIT\n\n");
......@@ -76,15 +76,27 @@ if (defined($optctl{'version'})) {
}
# Variables we may set below
my $loginUsername;
# Pull in hostname
my $hostSpec = shift(@ARGV) // "";
my ($host,$port) = split(':',$hostSpec);
if (!defined($host)) {
my ($loginHost,$loginPort) = split(':',$hostSpec);
if (defined($loginHost)) {
# Suck in username if specified
my ($userBit,$hostBit) = split(':',$loginHost);
if (defined($hostBit)) {
$loginUsername = $userBit;
$loginHost = $hostBit;
}
} else {
logger('ERROR',color('magenta')."No hostname provided".color('reset'));
exit 1;
}
# Check for config and read
my $configFile = $ENV{"HOME"}.'/.awit-ssh.conf';
if (! -f $configFile) {
......@@ -107,7 +119,6 @@ if (!defined($ldapBase) || $ldapBase eq "") {
# Check if we should be doing port knocking
if (defined(my $knock = $optctl{'knock'})) {
# If so, split off the host and the port
......@@ -171,7 +182,7 @@ my $mesg = $ldap->bind("cn=$username,ou=Users,$ldapBase",password => $password);
# Search
$mesg = $ldap->search(
base => "ou=Servers,$ldapBase",
filter => "(|(cn=$host)(awitLoginHost=$host)(awitLoginHostAlias=$host))",
filter => "(|(cn=$loginHost)(awitLoginHost=$loginHost)(awitLoginHostAlias=$loginHost))",
);
# Check for error
if (my $mesgCode = $mesg->code()) {
......@@ -186,7 +197,6 @@ if (my $mesgCode = $mesg->code()) {
# Some flags we may need
my $needDSS;
my $loginUsername;
# If no matches
......@@ -226,19 +236,19 @@ if ($ldapEntry) {
# Check if we need to set the host
if (my $ldapLoginHost = $ldapEntry->get_value('awitLoginHost')) {
logger('INFO'," - Host ".color('green')."%s".color('reset')." (awitLoginHost)",$ldapLoginHost);
$host = $ldapLoginHost;
$loginHost = $ldapLoginHost;
}
# Check if we need to set the port
if (my $ldapLoginPort = $ldapEntry->get_value('awitLoginPort')) {
logger('INFO'," - Port ".color('green')."%s".color('reset')." (awitLoginPort)",$ldapLoginPort);
$port = $ldapLoginPort;
$loginPort = $ldapLoginPort if (!defined($loginPort));
}
# Check if we need to set the username
if (my $ldapLoginUsername = $ldapEntry->get_value('awitLoginUsername')) {
logger('INFO'," - Username ".color('green')."%s".color('reset')." (awitLoginUsername)",$ldapLoginUsername);
$loginUsername = $ldapLoginUsername;
$loginUsername = $ldapLoginUsername if (!defined($loginUsername));
}
# Check if we have a description
......@@ -273,8 +283,8 @@ if ($ldapEntry) {
my @sshArgs = ();
# Check if we have a port defined, if so specify it
if (defined($port)) {
push(@sshArgs,'-p',$port);
if (defined($loginPort)) {
push(@sshArgs,'-p',$loginPort);
}
# Check if we have a different username defined to login as
......@@ -290,8 +300,8 @@ if (defined($needDSS)) {
push(@sshArgs,'-o','HostKeyAlgorithms=+ssh-dss');
}
logger('NOTICE',"Connecting to host '".color('green')."$host".color('reset')."'" .
(defined($port) ? " on port '".color('green')."$port".color('reset')."'" : "") . "...\n\n\n");
logger('NOTICE',"Connecting to host '".color('green')."$loginHost".color('reset')."'" .
(defined($loginPort) ? " on port '".color('green')."$loginPort".color('reset')."'" : "") . "...\n\n\n");
exec('/usr/bin/ssh',
'-F',$ENV{"HOME"}.'/.ssh/config',
......@@ -312,7 +322,7 @@ exec('/usr/bin/ssh',
'-o','ControlMaster=autoask',
'-o','ControlPath=~/.ssh/awit-ssh-master-%C',
@sshArgs,
$host
$loginHost
);
exit 0;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment