]> thyrsus.com Git - ssh-installkeys.git/commitdiff
Initial revision
authorEric S. Raymond <esr@thyrsus.com>
Thu, 14 Dec 2006 04:08:24 +0000 (04:08 +0000)
committerEric S. Raymond <esr@thyrsus.com>
Thu, 14 Dec 2006 04:08:24 +0000 (04:08 +0000)
sshauth [new file with mode: 0755]

diff --git a/sshauth b/sshauth
new file mode 100755 (executable)
index 0000000..fd337d4
--- /dev/null
+++ b/sshauth
@@ -0,0 +1,188 @@
+#!/bin/bash
+DOC= <<EOF
+This script tries to export ssh public keys to a specified site.  It will
+walk you through generating key pairs if it doesn't find any to export.
+It handles all the fiddly details like making sure local and remote
+permissions are correct, and tells you what it's doing if it has to change
+anything.
+
+Options:
+-h        Print usage summary and exit
+-c        Check local and remote configuration only, don't try to fix problems.
+-d        Delete remote .ssh configuration.
+-v        Report all commands sent to host and output received.
+-p <port> Connect on a particular port in place of 22
+
+Author: Eric S. Raymond <esr@thyrsus.com>
+EOF
+
+check=no
+verbose=""
+delete=no
+port=""
+quiet="-q"
+
+while getopts "cdp:hv" option
+do 
+    case $option in
+    c) check=yes ;;
+    d) delete=yes ;;
+    p) port="-p $OPTARG" ;;
+    h) print $DOC; exit 0;;
+    v) verbose=-v quiet="" ;;
+    esac
+done
+shift $(($OPTIND - 1))
+
+if [ "$1" = "" ]
+then
+    echo "No remote host specified."
+    echo $DOC
+    exit 0
+else
+    set -- `echo $1 | tr '@' ' '`
+    if [ "$2" = "" ]
+    then
+       user=$USER
+       host=$1
+    else
+       user=$1
+       host=$2
+    fi
+fi
+
+# Verify that key pairs are present on the local side 
+echo "Checking your local configuration..."
+if [ ! -d ${HOME} ]
+then
+    echo "Can't find your home directory!"
+    exit 1
+fi
+if [ ! -r ${HOME} ]
+then
+    echo "Your home directory is not readable by you. That's weird."
+    echo "This probably means \${HOME}=${HOME} is incorrect."
+    exit 1
+fi
+if [ ! -d ${HOME}/.ssh ]
+then
+    echo "You have no .ssh directory."
+    if [ $check = 'yes' ]
+    then
+       echo "Creation of ssh suppressed, exiting."
+       echo 1
+    else
+       mkdir $HOME/.ssh
+    fi
+fi
+if [ ! -r ${HOME}/.ssh ]
+then
+    echo "Your .ssh directory is not readable by you. That's weird."
+    echo "This probably means \$HOME=$HOME is incorrect."
+    exit 1
+fi
+if cd $HOME/.ssh
+then
+    : 
+else
+    echo "Can't chdir into your .ssh directory!"
+    exit 1
+fi
+public_keys=$(echo *.pub)              # Possible bash dependency
+if [ "$public_keys" = "" ]
+then
+    if [ $check = 'yes' ]
+    then
+       echo "I don't see any key pairs.  You need to generate some."
+       echo "Try re-running this script without the check-only option."
+       exit 1
+    fi
+    echo "About to generate public-key pairs for ssh versions 1 and 2."  
+    echo "You will be prompted for a passphrase for each."
+    echo "Pressing enter at the prompts will create keys with no passphrase."
+    ssh-keygen ${port} ${verbose} -t rsa1 -f identity
+    ssh-keygen ${port} ${verbose} -t dsa -t id_dsa
+    public_keys="identity.pub id_dsa.pub"
+fi
+echo "I see the following public keys: $public_keys"
+set -- `echo $public_keys | sed -e '/.pub$/s///g'`
+for private_key in $*
+do
+    legend="'${private_key}' corresponding to '${private_key}.pub'"
+    if [ -r $private_key ]
+    then
+       echo "$legend exists and is readable."
+    else
+       echo "$legend does not exist or is unreadable."
+       exit 1
+    fi
+done
+
+echo "Local configuration looks correct."
+
+if [ $delete = yes ]
+then
+    ssh ${port} ${verbose} -l ${user} ${host} "rm -fr .ssh"
+    "Keys for $user and $host have been deleted."
+    exit 0
+fi
+
+agent="
+#!/bin/bash
+# Agent script generated by sshauth.
+
+checkonly=$check
+
+function fixperms {
+    # Force perms of \$1 to user read on, group/other write off.
+    file=\$1
+    set -- \`/bin/ls -ld \$file\`
+    perms=\$1
+    if expr \$perms : ".r...-..-." >/dev/null  # Possible dependency on GNU expr
+    then
+       echo \"Remote permissions of \$file are \$perms, which is OK.\"
+    else
+       echo \"Remote permissions of \$file are \$perms, which is wrong.\"
+       echo \"User read should be on, Group and Other write permissions should be off.\"
+       if [ \$checkonly = 'no' ]
+       then
+           echo \"Correcting permissions...\"
+           /bin/chmod u+r,g-w,o-w \$file
+       fi
+    fi
+}
+
+fixperms .
+if [ ! -d .ssh ]
+then
+    echo \"Remote .ssh directory does not exist.\"
+    if [ \$checkonly = 'yes' ]
+    then
+       exit 1
+    else
+       echo \"Creating remote .ssh...\"
+       mkdir \~/.ssh
+    fi    
+fi
+fixperms .ssh
+
+if [ -x .ssh/authorized_keys ]
+then
+    echo \"Remote authorized_keys file will not be overwritten.\"
+    echo \"To remove it, do 'sshauth -d ${user}@$host}'.\"
+    exit 1
+else
+    echo \"Creating remote .ssh/authorized_keys file...\"
+fi
+"
+
+if echo "$agent" | ssh -T ${quiet} ${user}@${host}
+then
+    echo "Installation or check successful."
+else
+    echo "Installation or check failed."
+    exit 1
+fi
+
+
+