Repository: felipe-lavratti/ssh-allow-friend Branch: master Commit: aa59f367d570 Files: 5 Total size: 5.9 KB Directory structure: gitextract_6i_iabsu/ ├── .travis.yml ├── LICENSE.md ├── README.md ├── ssh-allow-friend └── test.sh ================================================ FILE CONTENTS ================================================ ================================================ FILE: .travis.yml ================================================ os: - linux - osx script: ./test.sh ================================================ FILE: LICENSE.md ================================================ Copyright (c) 2017: Felipe Lavratti Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: README.md ================================================ **ssh-allow-friend** This is a bash script to temporarily allow a ssh login using friends public key automatically fetched from online servers. Example usage: ```sh ./ssh-allow-friend --github my-friend-user-name ``` The script will fetch `my-friend-user-name`'s public key from github and add to current user authorized keys. After running the command, in a machine where $USER is `fanl` and the local ip is `192.168.2.13`, the script will print: ``` Acquired key for user my-friend-user-name from github, your friend is now able to login via ssh using: ssh fanl@192.168.2.13 Login authorization will be ceased after this program terminates. Press ^C to exit. ``` Your friend can now login with the command `fanl@192.168.2.13`. If you need to select a different user to your friend ssh session, use: ```sh sudo -H -u another_user ./ssh-allow-friend --github my-friend-user-name ``` ================================================ FILE: ssh-allow-friend ================================================ #!/bin/bash print_help () { echo "" echo " Temporarily allow a ssh login using friends public key" echo " fetched from online servers." echo "" echo "Usage:" echo " $0 [options] " echo "" echo "[options]" echo " -h | --help Print this help;" echo " -g | --github Select Github as public key server." echo "" echo "" echo " It is the username used to download keys from selected" echo " server." echo "" echo "Advanced example:" echo "" echo " Allow user \`john-doe\` from Github to login as user" echo " \`myself\`:" echo " sudo -H -u myself $0 --github john-doe" echo "" } print_usage () { echo "Usage example: $0 --github " } # # Configuration vars GITHUB=0 USERNAME='' USERKEY='' SERVICENAME='' LOCALIPADDRESS='' # # Test dependencies getopt --test > /dev/null if [[ $? -ne 4 ]]; then echo "$0: \`getopt --test\` failed in this environment." exit 1 fi curl --help > /dev/null if [[ $? -ne 0 ]]; then echo "$0: \`curl --help\` failed in this environment." exit 1 fi IP_CMD=$(which ip 2> /dev/null) if [[ $? -ne 0 ]]; then echo "$0: ip command not available." exit 1 fi SSH_KEYGEN_CMD=$(which ssh-keygen 2> /dev/null) if [[ $? -ne 0 ]]; then echo "$0: ssh-keygen command not available." exit 1 fi # # Parse arguments SHORT=gh LONG=github,help PARSED=`getopt --options $SHORT --longoptions $LONG --name "$0" -- "$@"` if [[ $? -ne 0 ]]; then # e.g. $? == 1 echo "$0: Invalid parameters." print_usage exit 2 fi eval set -- "$PARSED" while true; do case "$1" in -h|--help) print_help exit 0 ;; -g|--github) GITHUB=1 shift ;; --) shift break ;; *) echo "Programming error" exit 3 ;; esac done if [[ $# -ne 1 ]]; then echo "$0: A single user name is required." print_usage exit 4 fi USERNAME=$1 # # Get user keys from server github_download_key () { SERVICENAME='github' USERKEY=`curl -s https://github.com/$USERNAME.keys` if [[ -z $USERKEY ]]; then echo "User $USERNAME has no keys in Github." exit 5 fi if [[ "$USERKEY" == "Not Found" ]]; then echo "User $USERNAME not found in Github." exit 5 fi } if [[ $GITHUB -eq 1 ]]; then github_download_key else echo "$0: No service selected." print_usage exit 5 fi # # Check keys integrity TMP_KEY_FILE=/tmp/.ssh-allow-friend.$USERNAME-$USER-$SERVICENAME.keys echo $USERKEY > $TMP_KEY_FILE ssh-keygen -l -f $TMP_KEY_FILE > /dev/null if [[ $? -ne 0 ]]; then echo "$0: Downloaded key is invalid." exit 6 fi rm -f $TMP_KEY_FILE # # Get local ip address LOCALIPADDRESS=$($IP_CMD -o addr show scope global | awk '{gsub(/\/.*/,"",$4); print $4}') echo "Acquired key for user $USERNAME from $SERVICENAME," echo "your friend is now able to login via ssh using:" echo "$LOCALIPADDRESS" | while read a; do echo " ssh $USER@$a"; done echo "" echo "Login authorization will be ceased after this program" echo "terminates." echo "Press ^C to exit." setup () { ( flock 200 mkdir -p $HOME/.ssh/ echo "$USERKEY" >> $HOME/.ssh/authorized_keys ) 200>/tmp/.ssh-allow-friend.$USER.lock } teardown () { ( flock 200 # remove key from file, or the entire file if empty if grep -v "$USERKEY" $HOME/.ssh/authorized_keys > $HOME/.ssh/tmp; then cat $HOME/.ssh/tmp > $HOME/.ssh/authorized_keys && rm $HOME/.ssh/tmp; else rm $HOME/.ssh/authorized_keys && rm $HOME/.ssh/tmp; fi ) 200>/tmp/.ssh-allow-friend.$USER.lock } trap "teardown; exit 0" SIGHUP SIGINT SIGTERM setup sleep infinity & wait ================================================ FILE: test.sh ================================================ #!/bin/bash set -e ./ssh-allow-friend -g flplv & sleep 10 cat $HOME/.ssh/authorized_keys kill $!