Thomas Vachuska | 12bf445 | 2015-06-26 09:15:38 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # ----------------------------------------------------------------------------- |
Thomas Vachuska | 5af2e4f | 2016-12-16 12:07:33 -0800 | [diff] [blame] | 3 | # Adds or removes a user key for managing passwordless login to ONOS console. |
Thomas Vachuska | 12bf445 | 2015-06-26 09:15:38 -0700 | [diff] [blame] | 4 | # ----------------------------------------------------------------------------- |
| 5 | |
Thomas Vachuska | 5af2e4f | 2016-12-16 12:07:33 -0800 | [diff] [blame] | 6 | usage="usage: $(basename $0) user {key|key-file|--remove}" |
| 7 | |
| 8 | [ $# -lt 2 ] && echo "$usage" >&2 && exit 1 |
Thomas Vachuska | 12bf445 | 2015-06-26 09:15:38 -0700 | [diff] [blame] | 9 | |
| 10 | set -e |
| 11 | |
| 12 | user=$1 |
| 13 | [ -f $2 ] && key=$(cut -d\ -f2 $2) || key=$2 |
| 14 | |
Thomas Vachuska | 5af2e4f | 2016-12-16 12:07:33 -0800 | [diff] [blame] | 15 | [ -z "$user" -o -z "$key" ] && echo "$usage" >&2 && exit 1 |
| 16 | |
Thomas Vachuska | 12bf445 | 2015-06-26 09:15:38 -0700 | [diff] [blame] | 17 | cd $(dirname $0)/../apache-karaf-*/etc |
| 18 | KEYS=keys.properties |
| 19 | |
| 20 | # Remove the user key first, in case one was already present |
Thomas Vachuska | 5af2e4f | 2016-12-16 12:07:33 -0800 | [diff] [blame] | 21 | egrep -v "^($user|karaf)[ ]*=" $KEYS > $KEYS.new && mv $KEYS.new $KEYS |
| 22 | if [ $key != "--remove" ]; then |
Thomas Vachuska | 12bf445 | 2015-06-26 09:15:38 -0700 | [diff] [blame] | 23 | echo "$user=$key,_g_:admingroup" >> $KEYS |
| 24 | fi |
Thomas Vachuska | 5af2e4f | 2016-12-16 12:07:33 -0800 | [diff] [blame] | 25 | |
Jimmy Ou | 6daae94 | 2017-06-13 13:25:47 +0800 | [diff] [blame] | 26 | # ensure .ssh directory and known_hosts file exist |
| 27 | mkdir -p $HOME/.ssh/ |
Yuta HIGUCHI | 0410712 | 2017-01-13 18:51:12 -0800 | [diff] [blame] | 28 | (umask 077; touch "$HOME/.ssh/known_hosts") |
| 29 | |
Thomas Vachuska | 5af2e4f | 2016-12-16 12:07:33 -0800 | [diff] [blame] | 30 | # Also, remove any previous known keys for the localhost. |
| 31 | ssh-keygen -f "$HOME/.ssh/known_hosts" -R [localhost]:8101 |