Add modules to repository

This commit is contained in:
Sebastian Kinne
2017-11-16 16:42:22 +11:00
commit d0aa1e38ef
707 changed files with 96750 additions and 0 deletions

274
KeyManager/api/module.php Normal file
View File

@@ -0,0 +1,274 @@
<?php namespace pineapple;
putenv('LD_LIBRARY_PATH='.getenv('LD_LIBRARY_PATH').':/sd/lib:/sd/usr/lib');
putenv('PATH='.getenv('PATH').':/sd/usr/bin:/sd/usr/sbin');
class KeyManager extends Module
{
public function route()
{
switch ($this->request->action) {
case 'refreshInfo':
$this->refreshInfo();
break;
case 'refreshOutput':
$this->refreshOutput();
break;
case 'clearOutput':
$this->clearOutput();
break;
case 'refreshStatus':
$this->refreshStatus();
break;
case 'handleDependencies':
$this->handleDependencies();
break;
case 'handleDependenciesStatus':
$this->handleDependenciesStatus();
break;
case 'handleKey':
$this->handleKey();
break;
case 'handleKeyStatus':
$this->handleKeyStatus();
break;
case 'saveKnownHostsData':
$this->saveKnownHostsData();
break;
case 'getKnownHostsData':
$this->getKnownHostsData();
break;
case 'addToKnownHosts':
$this->addToKnownHosts();
break;
case 'addToKnownHostsStatus':
$this->addToKnownHostsStatus();
break;
case 'copyToRemoteHost':
$this->copyToRemoteHost();
break;
case 'copyToRemoteHostStatus':
$this->copyToRemoteHostStatus();
break;
case 'getSettings':
$this->getSettings();
break;
}
}
protected function checkDependency($dependencyName)
{
return ((exec("which {$dependencyName}") == '' ? false : true) && ($this->uciGet("keymanager.module.installed")));
}
protected function getDevice()
{
return trim(exec("cat /proc/cpuinfo | grep machine | awk -F: '{print $2}'"));
}
protected function refreshInfo()
{
$moduleInfo = @json_decode(file_get_contents("/pineapple/modules/KeyManager/module.info"));
$this->response = array('title' => $moduleInfo->title, 'version' => $moduleInfo->version);
}
private function handleKey()
{
if(!file_exists("/root/.ssh/id_rsa"))
{
$this->execBackground("/pineapple/modules/KeyManager/scripts/generate_key.sh");
$this->response = array('success' => true);
}
else
{
exec("rm -rf /root/.ssh/id_rsa*");
$this->response = array('success' => true);
}
}
private function handleKeyStatus()
{
if (!file_exists('/tmp/KeyManager_key.progress'))
{
$this->response = array('success' => true);
}
else
{
$this->response = array('success' => false);
}
}
private function handleDependencies()
{
if(!$this->checkDependency("ssh-keyscan"))
{
$this->execBackground("/pineapple/modules/KeyManager/scripts/dependencies.sh install ".$this->request->destination);
$this->response = array('success' => true);
}
else
{
$this->execBackground("/pineapple/modules/KeyManager/scripts/dependencies.sh remove");
$this->response = array('success' => true);
}
}
private function handleDependenciesStatus()
{
if (!file_exists('/tmp/KeyManager.progress'))
{
$this->response = array('success' => true);
}
else
{
$this->response = array('success' => false);
}
}
private function refreshStatus()
{
if (!file_exists('/tmp/KeyManager.progress'))
{
if(!$this->checkDependency("ssh-keyscan"))
{
$installed = false;
$install = "Not installed";
$installLabel = "danger";
$processing = false;
}
else
{
$installed = true;
$install = "Installed";
$installLabel = "success";
$processing = false;
}
if (!file_exists('/tmp/KeyManager_key.progress'))
{
if(!file_exists("/root/.ssh/id_rsa"))
{
$key = "Not generated";
$keyLabel = "danger";
$generated = false;
$generating = false;
}
else
{
$key = "Generated";
$keyLabel = "success";
$generated = true;
$generating = false;
}
}
else
{
$key = "Generating...";
$keyLabel = "warning";
$generated = false;
$generating = true;
}
}
else
{
$installed = false;
$install = "Installing...";
$installLabel = "warning";
$processing = true;
$key = "Not generated";
$keyLabel = "danger";
$generating = false;
}
$device = $this->getDevice();
$sdAvailable = $this->isSDAvailable();
$this->response = array("device" => $device, "sdAvailable" => $sdAvailable, "installed" => $installed, "key" => $key, "keyLabel" => $keyLabel, "generating" => $generating, "generated" => $generated, "install" => $install, "installLabel" => $installLabel, "processing" => $processing);
}
private function refreshOutput()
{
if (file_exists("/tmp/keymanager.log"))
{
$output = file_get_contents("/tmp/keymanager.log");
if(!empty($output))
$this->response = $output;
else
$this->response = " ";
}
else
{
$this->response = " ";
}
}
private function clearOutput()
{
exec("rm -rf /tmp/keymanager.log");
}
private function saveKnownHostsData()
{
$filename = '/root/.ssh/known_hosts';
file_put_contents($filename, $this->request->knownHostsData);
}
private function getKnownHostsData()
{
$knownHostsData = file_get_contents('/root/.ssh/known_hosts');
$this->response = array("knownHostsData" => $knownHostsData);
}
private function addToKnownHostsStatus()
{
if (!file_exists('/tmp/KeyManager.progress'))
{
$this->response = array('success' => true);
}
else
{
$this->response = array('success' => false);
}
}
private function addToKnownHosts()
{
$this->uciSet("keymanager.settings.host", $this->request->host);
$this->uciSet("keymanager.settings.port", $this->request->port);
$this->execBackground("/pineapple/modules/KeyManager/scripts/add_host.sh");
$this->response = array('success' => true);
}
private function copyToRemoteHostStatus()
{
if (!file_exists('/tmp/KeyManager.progress'))
{
$this->response = array('success' => true);
}
else
{
$this->response = array('success' => false);
}
}
private function copyToRemoteHost()
{
$this->uciSet("keymanager.settings.host", $this->request->host);
$this->uciSet("keymanager.settings.port", $this->request->port);
$this->uciSet("keymanager.settings.user", $this->request->user);
$this->execBackground("/pineapple/modules/KeyManager/scripts/copy_key.sh ".$this->request->password);
$this->response = array('success' => true);
}
private function getSettings()
{
$settings = array(
'host' => $this->uciGet("keymanager.settings.host"),
'port' => $this->uciGet("keymanager.settings.port"),
'user' => $this->uciGet("keymanager.settings.user")
);
$this->response = $settings;
}
}

298
KeyManager/js/module.js Normal file
View File

@@ -0,0 +1,298 @@
registerController('KeyManager_Controller', ['$api', '$scope', '$rootScope', '$interval', '$timeout', function($api, $scope, $rootScope, $interval, $timeout) {
$scope.title = "Loading...";
$scope.version = "Loading...";
$scope.refreshInfo = (function() {
$api.request({
module: 'KeyManager',
action: "refreshInfo"
}, function(response) {
$scope.title = response.title;
$scope.version = "v"+response.version;
})
});
$scope.refreshInfo();
}]);
registerController('KeyManager_ControlsController', ['$api', '$scope', '$rootScope', '$interval', '$timeout', function($api, $scope, $rootScope, $interval, $timeout) {
$scope.install = "Loading...";
$scope.installLabel = "default";
$scope.processing = false;
$scope.key = "Loading...";
$scope.keyLabel = "default";
$scope.generating = false;
$scope.device = '';
$scope.sdAvailable = false;
$rootScope.status = {
installed : false,
generated : false,
refreshOutput : false,
refreshKnownHosts : false
};
$scope.refreshStatus = (function() {
$api.request({
module: "KeyManager",
action: "refreshStatus"
}, function(response) {
$rootScope.status.installed = response.installed;
$scope.device = response.device;
$scope.sdAvailable = response.sdAvailable;
if(response.processing) $scope.processing = true;
$scope.install = response.install;
$scope.installLabel = response.installLabel;
$rootScope.status.generated = response.generated;
$scope.key = response.key;
if(response.generating) $scope.generating = true;
$scope.keyLabel = response.keyLabel;
})
});
$scope.handleKey = (function() {
if($scope.key != "Generated")
$scope.key = "Generating...";
else
$scope.key = "Removing...";
$api.request({
module: 'KeyManager',
action: 'handleKey'
}, function(response){
if (response.success === true) {
$scope.keyLabel = "warning";
$scope.generating = true;
$scope.handleKeyInterval = $interval(function(){
$api.request({
module: 'KeyManager',
action: 'handleKeyStatus'
}, function(response) {
if (response.success === true){
$scope.generating = false;
$interval.cancel($scope.handleKeyInterval);
$scope.refreshStatus();
}
});
}, 5000);
}
});
});
$scope.handleDependencies = (function(param) {
if(!$rootScope.status.installed)
$scope.install = "Installing...";
else
$scope.install = "Removing...";
$api.request({
module: 'KeyManager',
action: 'handleDependencies',
destination: param
}, function(response){
if (response.success === true) {
$scope.installLabel = "warning";
$scope.processing = true;
$scope.handleDependenciesInterval = $interval(function(){
$api.request({
module: 'KeyManager',
action: 'handleDependenciesStatus'
}, function(response) {
if (response.success === true){
$scope.processing = false;
$scope.refreshStatus();
$interval.cancel($scope.handleDependenciesInterval);
}
});
}, 5000);
}
});
});
$scope.refreshStatus();
}]);
registerController('KeyManager_OutputController', ['$api', '$scope', '$rootScope', '$interval', function($api, $scope, $rootScope, $interval) {
$scope.output = 'Loading...';
$scope.refreshOutput = (function() {
$api.request({
module: "KeyManager",
action: "refreshOutput",
filter: $scope.filter
}, function(response) {
$scope.output = response;
})
});
$scope.clearOutput = (function() {
$api.request({
module: "KeyManager",
action: "clearOutput"
}, function(response) {
$scope.refreshOutput();
})
});
$scope.refreshOutput();
$rootScope.$watch('status.refreshOutput', function(param) {
if(param) {
$scope.refreshOutput();
}
});
}]);
registerController('KeyManager_RemoteHostController', ['$api', '$scope', '$rootScope', '$interval', function($api, $scope, $rootScope, $interval) {
$scope.host = '';
$scope.port = '';
$scope.user = '';
$scope.password = '';
$scope.addHostLabel = "primary";
$scope.addHost = "Add remote host to local known_hosts";
$scope.copyKeyLabel = "primary";
$scope.copyKey = "Copy public key to remote host";
$scope.working = false;
$scope.addToKnownHosts = (function() {
$rootScope.status.refreshOutput = false;
$rootScope.status.refreshKnownHosts = false;
$api.request({
module: 'KeyManager',
action: 'addToKnownHosts',
host: $scope.host,
port: $scope.port
}, function(response) {
$scope.addHostLabel = "warning";
$scope.addHost = "Working...";
$scope.working = true;
$scope.addToKnownHostsInterval = $interval(function(){
$api.request({
module: 'KeyManager',
action: 'addToKnownHostsStatus'
}, function(response) {
if (response.success === true){
$scope.working = false;
$interval.cancel($scope.addToKnownHostsInterval);
$scope.addHostLabel = "primary";
$scope.addHost = "Add remote host to local known_hosts";
$rootScope.status.refreshOutput = true;
$rootScope.status.refreshKnownHosts = true;
}
});
}, 5000);
});
});
$scope.copyToRemoteHost = (function() {
$rootScope.status.refreshOutput = false;
$rootScope.status.refreshKnownHosts = false;
$api.request({
module: 'KeyManager',
action: 'copyToRemoteHost',
host: $scope.host,
port: $scope.port,
user: $scope.user,
password: $scope.password
}, function(response) {
$scope.copyKeyLabel = "warning";
$scope.copyKey = "Working...";
$scope.working = true;
$scope.copyToRemoteHostInterval = $interval(function(){
$api.request({
module: 'KeyManager',
action: 'copyToRemoteHostStatus'
}, function(response) {
if (response.success === true){
$scope.working = false;
$interval.cancel($scope.copyToRemoteHostInterval);
$scope.copyKeyLabel = "primary";
$scope.copyKey = "Copy public key to remote host";
$rootScope.status.refreshOutput = true;
$rootScope.status.refreshKnownHosts = true;
}
});
}, 5000);
});
});
$scope.getSettings = function() {
$api.request({
module: 'KeyManager',
action: 'getSettings'
}, function(response) {
$scope.host = response.host;
$scope.port = response.port;
$scope.user = response.user;
});
};
$scope.getSettings();
}]);
registerController('KeyManager_KnownHostsController', ['$api', '$scope', '$timeout', '$rootScope', function($api, $scope, $timeout, $rootScope) {
$scope.knownHostsData = '';
$scope.saveKnownHostsLabel = "primary";
$scope.saveKnownHosts = "Save";
$scope.saving = false;
$scope.saveKnownHostsData = (function() {
$scope.saveKnownHostsLabel = "warning";
$scope.saveKnownHosts = "Saving...";
$scope.saving = true;
$api.request({
module: 'KeyManager',
action: 'saveKnownHostsData',
knownHostsData: $scope.knownHostsData
}, function(response) {
$scope.saveKnownHostsLabel = "success";
$scope.saveKnownHosts = "Saved";
$timeout(function(){
$scope.saveKnownHostsLabel = "primary";
$scope.saveKnownHosts = "Save";
$scope.saving = false;
}, 2000);
});
});
$scope.getKnownHostsData = (function() {
$api.request({
module: 'KeyManager',
action: 'getKnownHostsData'
}, function(response) {
$scope.knownHostsData = response.knownHostsData;
});
});
$scope.getKnownHostsData();
$rootScope.$watch('status.refreshKnownHosts', function(param) {
if(param) {
$scope.getKnownHostsData();
}
});
}]);

132
KeyManager/module.html Normal file
View File

@@ -0,0 +1,132 @@
<div class="panel panel-default" ng-controller="KeyManager_Controller"><div class="panel-heading"><h4 class="panel-title pull-left">{{title}}</h4><span class="pull-right">{{version}}</span><div class="clearfix"></div></div></div>
<div class="row">
<div class="col-md-4">
<div class="panel panel-default" ng-controller="KeyManager_ControlsController">
<div class="panel-heading">
<h3 class="panel-title">Controls</h3>
</div>
<div class="panel-body">
<table style="width:100%">
<tr>
<td style="padding-bottom: .5em;" class="text-muted">Dependencies</td>
<td ng-hide="$root.status.installed" style="text-align:right;padding-bottom: .5em;"><button type="button" style="width: 90px;" class="btn btn-{{installLabel}} btn-xs" data-toggle="modal" data-target="#dependenciesInstallModal" ng-disabled="processing">{{install}}</button></td>
<td ng-show="$root.status.installed" style="text-align:right;padding-bottom: .5em;"><button type="button" style="width: 90px;" class="btn btn-{{installLabel}} btn-xs" data-toggle="modal" data-target="#dependenciesRemoveModal" ng-disabled="processing">{{install}}</button></td>
</tr>
<tr ng-show="$root.status.installed">
<td style="padding-bottom: .5em;" class="text-muted">SSH Key Pair</td>
<td style="text-align:right;padding-bottom: .5em;"><button type="button" style="width: 90px;" class="btn btn-{{keyLabel}} btn-xs" ng-disabled="generating" ng-click="handleKey()">{{key}}</button></td>
</tr>
</table>
</div>
<div class="modal fade" id="dependenciesInstallModal" tabindex="-1" role="dialog" aria-labelledby="dependenciesModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title" id="dependenciesInstallModalLabel">Install dependencies</h4>
</div>
<div class="modal-body">
All required dependencies have to be installed first. This may take a few minutes.<br /><br />
Please wait, do not leave or refresh this page. Once the install is complete, this page will refresh automatically.
</div>
<div class="modal-footer">
<button type="button" class="btn btn-info" ng-click="handleDependencies('internal')" data-dismiss="modal">Internal</button>
<button type="button" class="btn btn-info" ng-hide="device == 'tetra' || sdAvailable == false" ng-click="handleDependencies('sd')" data-dismiss="modal">SD Card</button>
</div>
</div>
</div>
</div>
<div class="modal fade" id="dependenciesRemoveModal" tabindex="-1" role="dialog" aria-labelledby="dependenciesModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title" id="dependenciesRemoveModalLabel">Remove dependencies</h4>
</div>
<div class="modal-body">
All required dependencies will be removed. This may take a few minutes.<br /><br />
Please wait, do not leave or refresh this page. Once the remove is complete, this page will refresh automatically.
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-info" ng-click="handleDependencies()" data-dismiss="modal">Confirm</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="panel panel-default" ng-show="$root.status.installed && $root.status.generated" ng-controller="KeyManager_RemoteHostController">
<div class="panel-heading">
<h4 class="panel-title">Remote Host</h4>
</div>
<div class="panel-body">
<div class="form-group">
<div class="input-group">
<span class="input-group-addon input-sm">Remote Host</span>
<input type="text" class="form-control input-sm" ng-model="host" placeholder="Remote SSH Server Host">
</div>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon input-sm">Remote Port</span>
<input type="text" class="form-control input-sm" ng-model="port" placeholder="Remote SSH Server Port (Typically 22)">
</div>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon input-sm">Remote User</span>
<input type="text" class="form-control input-sm" ng-model="user" placeholder="Remote SSH Server User">
</div>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon input-sm">Remote Password</span>
<input type="password" class="form-control input-sm" ng-model="password" placeholder="Remote SSH Server Password">
</div>
</div>
<button type="submit" class="btn btn-{{addHostLabel}} btn-sm" ng-disabled="working || (host == '' || port == '' )" ng-click="addToKnownHosts()">{{addHost}}</button>
<button type="submit" class="btn btn-{{copyKeyLabel}} btn-sm" ng-disabled="working || (host == '' || port == '' || user == '' || password == '')" ng-click="copyToRemoteHost()">{{copyKey}}</button>
</div>
</div>
<div class="panel panel-default" ng-show="$root.status.installed" ng-controller="KeyManager_KnownHostsController">
<div class="panel-heading pointer" data-toggle="collapse" data-target="#KnownHosts">
<h4 class="panel-title">Known Hosts</h4>
</div>
<div id="KnownHosts" class="panel-collapse collapse">
<div class="panel-body">
<button type="submit" class="btn btn-{{saveKnownHostsLabel}} btn-sm pull-right" ng-disabled="saving" ng-click="saveKnownHostsData()">{{saveKnownHosts}}</button><div class="clearfix"></div>
<form class="form-horizontal">
<textarea class="form-control" rows="20" ng-model="knownHostsData"></textarea>
</form>
</div>
</div>
</div>
<div class="panel panel-default" ng-show="$root.status.installed" ng-controller="KeyManager_OutputController">
<div class="panel-heading">
<h4 class="panel-title">Output</h4>
</div>
<div class="panel-body">
<div class="btn-group pull-right">
<button class="btn btn-danger btn-sm" ng-click="clearOutput()">Clear Log</button>
</div>
<div class="clearfix"></div>
<pre class="scrollable-pre log-pre">{{output}}</pre>
</div>
</div>

10
KeyManager/module.info Normal file
View File

@@ -0,0 +1,10 @@
{
"author": "Whistle Master",
"description": "SSH Key Manager",
"devices": [
"nano",
"tetra"
],
"title": "Key Manager",
"version": "1.0"
}

25
KeyManager/scripts/add_host.sh Executable file
View File

@@ -0,0 +1,25 @@
#!/bin/sh
#2015 - Whistle Master
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/sd/lib:/sd/usr/lib
export PATH=$PATH:/sd/usr/bin:/sd/usr/sbin
[[ -f /tmp/KeyManager.progress ]] && {
exit 0
}
LOG=/tmp/keymanager.log
HOST=`uci get keymanager.settings.host`
PORT=`uci get keymanager.settings.port`
touch /tmp/KeyManager.progress
rm -rf ${LOG}
ssh-keyscan -p ${PORT} ${HOST} > /tmp/tmp_hosts
cat /tmp/tmp_hosts >> /root/.ssh/known_hosts
echo -e "Added the following to /root/.ssh/known_hosts:" > ${LOG}
cat /tmp/tmp_hosts >> ${LOG}
rm /tmp/KeyManager.progress

34
KeyManager/scripts/copy_key.sh Executable file
View File

@@ -0,0 +1,34 @@
#!/bin/sh
#2015 - Whistle Master
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/sd/lib:/sd/usr/lib
export PATH=$PATH:/sd/usr/bin:/sd/usr/sbin
[[ -f /tmp/KeyManager.progress ]] && {
exit 0
}
LOG=/tmp/keymanager.log
HOST=`uci get keymanager.settings.host`
PORT=`uci get keymanager.settings.port`
USER=`uci get keymanager.settings.user`
PASSWORD=$1
touch /tmp/KeyManager.progress
rm -rf ${LOG}
if ! grep -q ${HOST} /root/.ssh/known_hosts; then
echo -e "Cannot find ${HOST} in known_hosts. Adding it now." > ${LOG}
ssh-keyscan -p ${PORT} ${HOST} > /tmp/tmp_hosts
cat /tmp/tmp_hosts >> /root/.ssh/known_hosts
echo -e "Added the following to /root/.ssh/known_hosts:" >> ${LOG}
cat /tmp/tmp_hosts >> ${LOG}
fi
sshpass -p ${PASSWORD} /pineapple/modules/KeyManager/scripts/ssh-copy-id.sh -i /root/.ssh/id_rsa.pub -p ${PORT} ${USER}@${HOST}
echo -e "Copied local public key to remote host." >> ${LOG}
rm /tmp/KeyManager.progress

View File

@@ -0,0 +1,42 @@
#!/bin/sh
#2015 - Whistle Master
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/sd/lib:/sd/usr/lib
export PATH=$PATH:/sd/usr/bin:/sd/usr/sbin
[[ -f /tmp/KeyManager.progress ]] && {
exit 0
}
touch /tmp/KeyManager.progress
if [ "$1" = "install" ]; then
if [ "$2" = "internal" ]; then
opkg update
opkg install openssh-client-utils
opkg install sshpass
elif [ "$2" = "sd" ]; then
opkg update
opkg install openssh-client-utils --dest sd
opkg install sshpass --dest sd
fi
if [ ! -f /root/.ssh/known_hosts ]; then
touch /root/.ssh/known_hosts
fi
touch /etc/config/keymanager
echo "config keymanager 'module'" > /etc/config/keymanager
echo "config keymanager 'settings'" >> /etc/config/keymanager
uci set keymanager.module.installed=1
uci commit keymanager.module.installed
elif [ "$1" = "remove" ]; then
opkg remove openssh-client-utils
opkg remove sshpass
rm -rf /etc/config/keymanager
fi
rm /tmp/KeyManager.progress

View File

@@ -0,0 +1,15 @@
#!/bin/sh
#2015 - Whistle Master
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/sd/lib:/sd/usr/lib
export PATH=$PATH:/sd/usr/bin:/sd/usr/sbin
[[ -f /tmp/KeyManager_key.progress ]] && {
exit 0
}
touch /tmp/KeyManager_key.progress
ssh-keygen -N "" -f /root/.ssh/id_rsa
rm /tmp/KeyManager_key.progress

300
KeyManager/scripts/ssh-copy-id.sh Executable file
View File

@@ -0,0 +1,300 @@
#!/bin/sh
# Copyright (c) 1999-2013 Philip Hands <phil@hands.com>
# 2013 Martin Kletzander <mkletzan@redhat.com>
# 2010 Adeodato =?iso-8859-1?Q?Sim=F3?= <asp16@alu.ua.es>
# 2010 Eric Moret <eric.moret@gmail.com>
# 2009 Xr <xr@i-jeuxvideo.com>
# 2007 Justin Pryzby <justinpryzby@users.sourceforge.net>
# 2004 Reini Urban <rurban@x-ray.at>
# 2003 Colin Watson <cjwatson@debian.org>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. 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 AUTHOR ``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 AUTHOR 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.
# Shell script to install your public key(s) on a remote machine
# See the ssh-copy-id(1) man page for details
# check that we have something mildly sane as our shell, or try to find something better
if false ^ printf "%s: WARNING: ancient shell, hunting for a more modern one... " "$0"
then
SANE_SH=${SANE_SH:-/usr/bin/ksh}
if printf 'true ^ false\n' | "$SANE_SH"
then
printf "'%s' seems viable.\n" "$SANE_SH"
exec "$SANE_SH" "$0" "$@"
else
cat <<-EOF
oh dear.
If you have a more recent shell available, that supports \$(...) etc.
please try setting the environment variable SANE_SH to the path of that
shell, and then retry running this script. If that works, please report
a bug describing your setup, and the shell you used to make it work.
EOF
printf "%s: ERROR: Less dimwitted shell required.\n" "$0"
exit 1
fi
fi
DEFAULT_PUB_ID_FILE=$(ls -t ${HOME}/.ssh/id*.pub 2>/dev/null | grep -v -- '-cert.pub$' | head -n 1)
usage () {
printf 'Usage: %s [-h|-?|-n] [-i [identity_file]] [-p port] [[-o <ssh -o options>] ...] [user@]hostname\n' "$0" >&2
exit 1
}
# escape any single quotes in an argument
quote() {
printf "%s\n" "$1" | sed -e "s/'/'\\\\''/g"
}
use_id_file() {
local L_ID_FILE="$1"
if expr "$L_ID_FILE" : ".*\.pub$" >/dev/null ; then
PUB_ID_FILE="$L_ID_FILE"
else
PUB_ID_FILE="$L_ID_FILE.pub"
fi
PRIV_ID_FILE=$(dirname "$PUB_ID_FILE")/$(basename "$PUB_ID_FILE" .pub)
# check that the files are readable
for f in $PUB_ID_FILE $PRIV_ID_FILE ; do
ErrMSG=$( { : < $f ; } 2>&1 ) || {
printf "\n%s: ERROR: failed to open ID file '%s': %s\n\n" "$0" "$f" "$(printf "%s\n" "$ErrMSG" | sed -e 's/.*: *//')"
exit 1
}
done
GET_ID="cat \"$PUB_ID_FILE\""
}
if [ -n "$SSH_AUTH_SOCK" ] && ssh-add -L >/dev/null 2>&1 ; then
GET_ID="ssh-add -L"
fi
while test "$#" -gt 0
do
[ "${SEEN_OPT_I}" ] && expr "$1" : "[-]i" >/dev/null && {
printf "\n%s: ERROR: -i option must not be specified more than once\n\n" "$0"
usage
}
OPT= OPTARG=
# implement something like getopt to avoid Solaris pain
case "$1" in
-i?*|-o?*|-p?*)
OPT="$(printf -- "$1"|cut -c1-2)"
OPTARG="$(printf -- "$1"|cut -c3-)"
shift
;;
-o|-p)
OPT="$1"
OPTARG="$2"
shift 2
;;
-i)
OPT="$1"
test "$#" -le 2 || expr "$2" : "[-]" >/dev/null || {
OPTARG="$2"
shift
}
shift
;;
-n|-h|-\?)
OPT="$1"
OPTARG=
shift
;;
--)
shift
while test "$#" -gt 0
do
SAVEARGS="${SAVEARGS:+$SAVEARGS }'$(quote "$1")'"
shift
done
break
;;
-*)
printf "\n%s: ERROR: invalid option (%s)\n\n" "$0" "$1"
usage
;;
*)
SAVEARGS="${SAVEARGS:+$SAVEARGS }'$(quote "$1")'"
shift
continue
;;
esac
case "$OPT" in
-i)
SEEN_OPT_I="yes"
use_id_file "${OPTARG:-$DEFAULT_PUB_ID_FILE}"
;;
-o|-p)
SSH_OPTS="${SSH_OPTS:+$SSH_OPTS }$OPT '$(quote "$OPTARG")'"
;;
-n)
DRY_RUN=1
;;
-h|-\?)
usage
;;
esac
done
eval set -- "$SAVEARGS"
if [ $# = 0 ] ; then
usage
fi
if [ $# != 1 ] ; then
printf '%s: ERROR: Too many arguments. Expecting a target hostname, got: %s\n\n' "$0" "$SAVEARGS" >&2
usage
fi
# drop trailing colon
USER_HOST=$(printf "%s\n" "$1" | sed 's/:$//')
# tack the hostname onto SSH_OPTS
SSH_OPTS="${SSH_OPTS:+$SSH_OPTS }'$(quote "$USER_HOST")'"
# and populate "$@" for later use (only way to get proper quoting of options)
eval set -- "$SSH_OPTS"
if [ -z "$(eval $GET_ID)" ] && [ -r "${PUB_ID_FILE:=$DEFAULT_PUB_ID_FILE}" ] ; then
use_id_file "$PUB_ID_FILE"
fi
if [ -z "$(eval $GET_ID)" ] ; then
printf '%s: ERROR: No identities found\n' "$0" >&2
exit 1
fi
# populate_new_ids() uses several global variables ($USER_HOST, $SSH_OPTS ...)
# and has the side effect of setting $NEW_IDS
populate_new_ids() {
local L_SUCCESS="$1"
# repopulate "$@" inside this function
eval set -- "$SSH_OPTS"
umask 0177
local L_TMP_ID_FILE=$(mktemp ~/.ssh/ssh-copy-id_id.XXXXXXXXXX)
if test $? -ne 0 || test "x$L_TMP_ID_FILE" = "x" ; then
echo "mktemp failed" 1>&2
exit 1
fi
trap "rm -f $L_TMP_ID_FILE ${L_TMP_ID_FILE}.pub" EXIT TERM INT QUIT
printf '%s: INFO: attempting to log in with the new key(s), to filter out any that are already installed\n' "$0" >&2
NEW_IDS=$(
eval $GET_ID | {
while read ID ; do
printf '%s\n' "$ID" > $L_TMP_ID_FILE
# the next line assumes $PRIV_ID_FILE only set if using a single id file - this
# assumption will break if we implement the possibility of multiple -i options.
# The point being that if file based, ssh needs the private key, which it cannot
# find if only given the contents of the .pub file in an unrelated tmpfile
ssh -i "${PRIV_ID_FILE:-$L_TMP_ID_FILE}" \
-o PreferredAuthentications=publickey \
-o IdentitiesOnly=yes "$@" exit 2>$L_TMP_ID_FILE.stderr </dev/null
if [ "$?" = "$L_SUCCESS" ] ; then
: > $L_TMP_ID_FILE
else
grep 'Permission denied' $L_TMP_ID_FILE.stderr >/dev/null || {
sed -e 's/^/ERROR: /' <$L_TMP_ID_FILE.stderr >$L_TMP_ID_FILE
cat >/dev/null #consume the other keys, causing loop to end
}
fi
cat $L_TMP_ID_FILE
done
}
)
rm -f $L_TMP_ID_FILE* && trap - EXIT TERM INT QUIT
if expr "$NEW_IDS" : "^ERROR: " >/dev/null ; then
printf '\n%s: %s\n\n' "$0" "$NEW_IDS" >&2
exit 1
fi
if [ -z "$NEW_IDS" ] ; then
printf '\n%s: WARNING: All keys were skipped because they already exist on the remote system.\n\n' "$0" >&2
exit 0
fi
printf '%s: INFO: %d key(s) remain to be installed -- if you are prompted now it is to install the new keys\n' "$0" "$(printf '%s\n' "$NEW_IDS" | wc -l)" >&2
}
REMOTE_VERSION=$(ssh -v -o PreferredAuthentications=',' "$@" 2>&1 |
sed -ne 's/.*remote software version //p')
case "$REMOTE_VERSION" in
NetScreen*)
populate_new_ids 1
for KEY in $(printf "%s" "$NEW_IDS" | cut -d' ' -f2) ; do
KEY_NO=$(($KEY_NO + 1))
printf "%s\n" "$KEY" | grep ssh-dss >/dev/null || {
printf '%s: WARNING: Non-dsa key (#%d) skipped (NetScreen only supports DSA keys)\n' "$0" "$KEY_NO" >&2
continue
}
[ "$DRY_RUN" ] || printf 'set ssh pka-dsa key %s\nsave\nexit\n' "$KEY" | ssh -T "$@" >/dev/null 2>&1
if [ $? = 255 ] ; then
printf '%s: ERROR: installation of key #%d failed (please report a bug describing what caused this, so that we can make this message useful)\n' "$0" "$KEY_NO" >&2
else
ADDED=$(($ADDED + 1))
fi
done
if [ -z "$ADDED" ] ; then
exit 1
fi
;;
*)
# Assuming that the remote host treats ~/.ssh/authorized_keys as one might expect
populate_new_ids 0
[ "$DRY_RUN" ] || printf '%s\n' "$NEW_IDS" | ssh "$@" "
umask 077 ;
mkdir -p .ssh && cat >> .ssh/authorized_keys || exit 1 ;
if type restorecon >/dev/null 2>&1 ; then restorecon -F .ssh .ssh/authorized_keys ; fi" \
|| exit 1
ADDED=$(printf '%s\n' "$NEW_IDS" | wc -l)
;;
esac
if [ "$DRY_RUN" ] ; then
cat <<-EOF
=-=-=-=-=-=-=-=
Would have added the following key(s):
$NEW_IDS
=-=-=-=-=-=-=-=
EOF
else
cat <<-EOF
Number of key(s) added: $ADDED
Now try logging into the machine, with: "ssh $SSH_OPTS"
and check to make sure that only the key(s) you wanted were added.
EOF
fi
# =-=-=-=