├── .travis.yml ├── LICENSE.md ├── README.md ├── ssh-allow-friend └── test.sh /.travis.yml: -------------------------------------------------------------------------------- 1 | os: 2 | - linux 3 | - osx 4 | 5 | script: ./test.sh 6 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright (c) 2017: Felipe Lavratti 2 | 3 | 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: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | 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. 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | **ssh-allow-friend** 2 | 3 | This is a bash script to temporarily allow a ssh login using friends public key 4 | automatically fetched from online servers. 5 | 6 | Example usage: 7 | ```sh 8 | ./ssh-allow-friend --github my-friend-user-name 9 | ``` 10 | 11 | The script will fetch `my-friend-user-name`'s public key from github and add to 12 | current user authorized keys. After running the command, in a machine where 13 | $USER is `fanl` and the local ip is `192.168.2.13`, the script will print: 14 | 15 | ``` 16 | Acquired key for user my-friend-user-name from github, 17 | your friend is now able to login via ssh using: 18 | ssh fanl@192.168.2.13 19 | 20 | Login authorization will be ceased after this 21 | program terminates. 22 | Press ^C to exit. 23 | ``` 24 | 25 | Your friend can now login with the command `fanl@192.168.2.13`. 26 | 27 | If you need to select a different user to your friend ssh session, use: 28 | 29 | ```sh 30 | sudo -H -u another_user ./ssh-allow-friend --github my-friend-user-name 31 | ``` 32 | -------------------------------------------------------------------------------- /ssh-allow-friend: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | print_help () { 4 | echo "" 5 | echo " Temporarily allow a ssh login using friends public key" 6 | echo " fetched from online servers." 7 | echo "" 8 | echo "Usage:" 9 | echo " $0 [options] " 10 | echo "" 11 | echo "[options]" 12 | echo " -h | --help Print this help;" 13 | echo " -g | --github Select Github as public key server." 14 | echo "" 15 | echo "" 16 | echo " It is the username used to download keys from selected" 17 | echo " server." 18 | echo "" 19 | echo "Advanced example:" 20 | echo "" 21 | echo " Allow user \`john-doe\` from Github to login as user" 22 | echo " \`myself\`:" 23 | echo " sudo -H -u myself $0 --github john-doe" 24 | echo "" 25 | } 26 | 27 | print_usage () { 28 | echo "Usage example: $0 --github " 29 | } 30 | 31 | # 32 | # Configuration vars 33 | GITHUB=0 34 | USERNAME='' 35 | USERKEY='' 36 | SERVICENAME='' 37 | LOCALIPADDRESS='' 38 | 39 | # 40 | # Test dependencies 41 | getopt --test > /dev/null 42 | if [[ $? -ne 4 ]]; then 43 | echo "$0: \`getopt --test\` failed in this environment." 44 | exit 1 45 | fi 46 | 47 | curl --help > /dev/null 48 | if [[ $? -ne 0 ]]; then 49 | echo "$0: \`curl --help\` failed in this environment." 50 | exit 1 51 | fi 52 | 53 | IP_CMD=$(which ip 2> /dev/null) 54 | if [[ $? -ne 0 ]]; then 55 | echo "$0: ip command not available." 56 | exit 1 57 | fi 58 | 59 | SSH_KEYGEN_CMD=$(which ssh-keygen 2> /dev/null) 60 | if [[ $? -ne 0 ]]; then 61 | echo "$0: ssh-keygen command not available." 62 | exit 1 63 | fi 64 | 65 | # 66 | # Parse arguments 67 | SHORT=gh 68 | LONG=github,help 69 | 70 | PARSED=`getopt --options $SHORT --longoptions $LONG --name "$0" -- "$@"` 71 | if [[ $? -ne 0 ]]; then 72 | # e.g. $? == 1 73 | echo "$0: Invalid parameters." 74 | print_usage 75 | exit 2 76 | fi 77 | eval set -- "$PARSED" 78 | 79 | while true; do 80 | case "$1" in 81 | -h|--help) 82 | print_help 83 | exit 0 84 | ;; 85 | -g|--github) 86 | GITHUB=1 87 | shift 88 | ;; 89 | --) 90 | shift 91 | break 92 | ;; 93 | *) 94 | echo "Programming error" 95 | exit 3 96 | ;; 97 | esac 98 | done 99 | 100 | if [[ $# -ne 1 ]]; then 101 | echo "$0: A single user name is required." 102 | print_usage 103 | exit 4 104 | fi 105 | 106 | USERNAME=$1 107 | 108 | # 109 | # Get user keys from server 110 | github_download_key () { 111 | SERVICENAME='github' 112 | USERKEY=`curl -s https://github.com/$USERNAME.keys` 113 | 114 | if [[ -z $USERKEY ]]; then 115 | echo "User $USERNAME has no keys in Github." 116 | exit 5 117 | fi 118 | 119 | if [[ "$USERKEY" == "Not Found" ]]; then 120 | echo "User $USERNAME not found in Github." 121 | exit 5 122 | fi 123 | } 124 | 125 | if [[ $GITHUB -eq 1 ]]; then 126 | github_download_key 127 | else 128 | echo "$0: No service selected." 129 | print_usage 130 | exit 5 131 | fi 132 | 133 | # 134 | # Check keys integrity 135 | TMP_KEY_FILE=/tmp/.ssh-allow-friend.$USERNAME-$USER-$SERVICENAME.keys 136 | echo $USERKEY > $TMP_KEY_FILE 137 | ssh-keygen -l -f $TMP_KEY_FILE > /dev/null 138 | if [[ $? -ne 0 ]]; then 139 | echo "$0: Downloaded key is invalid." 140 | exit 6 141 | fi 142 | rm -f $TMP_KEY_FILE 143 | 144 | 145 | # 146 | # Get local ip address 147 | LOCALIPADDRESS=$($IP_CMD -o addr show scope global | awk '{gsub(/\/.*/,"",$4); print $4}') 148 | 149 | echo "Acquired key for user $USERNAME from $SERVICENAME," 150 | echo "your friend is now able to login via ssh using:" 151 | echo "$LOCALIPADDRESS" | while read a; do echo " ssh $USER@$a"; done 152 | echo "" 153 | echo "Login authorization will be ceased after this program" 154 | echo "terminates." 155 | echo "Press ^C to exit." 156 | 157 | setup () { 158 | ( 159 | flock 200 160 | 161 | mkdir -p $HOME/.ssh/ 162 | echo "$USERKEY" >> $HOME/.ssh/authorized_keys 163 | ) 200>/tmp/.ssh-allow-friend.$USER.lock 164 | } 165 | 166 | teardown () { 167 | ( 168 | flock 200 169 | 170 | # remove key from file, or the entire file if empty 171 | if grep -v "$USERKEY" $HOME/.ssh/authorized_keys > $HOME/.ssh/tmp; then 172 | cat $HOME/.ssh/tmp > $HOME/.ssh/authorized_keys && rm $HOME/.ssh/tmp; 173 | else 174 | rm $HOME/.ssh/authorized_keys && rm $HOME/.ssh/tmp; 175 | fi 176 | ) 200>/tmp/.ssh-allow-friend.$USER.lock 177 | } 178 | 179 | trap "teardown; exit 0" SIGHUP SIGINT SIGTERM 180 | setup 181 | sleep infinity & 182 | wait 183 | -------------------------------------------------------------------------------- /test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -e 3 | 4 | ./ssh-allow-friend -g flplv & 5 | sleep 10 6 | cat $HOME/.ssh/authorized_keys 7 | kill $! 8 | --------------------------------------------------------------------------------