~fboucault/qtcreator-plugin-ubuntu/newarch_arm64

« back to all changes in this revision

Viewing changes to share/qtcreator/ubuntu/scripts/openssh_publickey_create

  • Committer: Benjamin Zeller
  • Date: 2016-06-08 15:09:39 UTC
  • mfrom: (443.2.36 ubuntu)
  • Revision ID: benjamin.zeller@canonical.com-20160608150939-v4ffv2a9xy5lcr5s
LXD rewrite for building and running apps in LXD containers

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/bin/bash
2
 
# Copyright 2013 Canonical Ltd.
 
2
# Copyright 2016 Canonical Ltd.
3
3
#
4
4
# This program is free software; you can redistribute it and/or modify
5
5
# it under the terms of the GNU Lesser General Public License as published by
14
14
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
15
#
16
16
# Author: Juhapekka Piiroinen <juhapekka.piiroinen@canonical.com>
17
 
 
18
 
. `dirname $0`/functions.inc
19
 
 
20
 
USERNAME=$2
 
17
# Author: Benjamin Zeller <benjamin.zeller@canonical.com>
 
18
 
 
19
if [ -z "$USDK_CONF_DIR" ]; then
 
20
    echo "USDK_CONF_DIR must be set in order for this scripts to work" 
 
21
    exit 1
 
22
else
 
23
    CONFIGDIR=$USDK_CONF_DIR
 
24
fi
 
25
 
 
26
SSHIDENTITY=${CONFIGDIR}/ubuntudevice_id_rsa
 
27
 
21
28
MIN_KEYLEN="2048"
22
29
 
23
 
if [ -z "$USERNAME" ]; then
24
 
    echo "Username missing"
25
 
    echo "Usage: openssh_publickey <deviceId> <username>"
26
 
    exit 1
27
 
fi
28
 
 
29
30
function check_key {
30
31
    printf "Checking keylen.."
31
32
    KEY_LEN=$(ssh-keygen -l -f $SSHIDENTITY.pub | cut -f1 -d " ")
34
35
        echo "SSH key len ${KEY_LEN} is too weak. Creating a new one"
35
36
        generate_key
36
37
        deploy_key
37
 
    else
38
 
        echo "Checking for keys.."
39
 
        KEY=`cat $SSHIDENTITY.pub`
40
 
        set +e
41
 
        PHABLET_KEYS=`adb_shell "cat /home/$USERNAME/.ssh/authorized_keys2"`
42
 
        KEYS=`echo $PHABLET_KEYS | grep "$KEY"`
43
 
        set -e
44
 
        echo
45
 
        echo "Host key is:"
46
 
        echo $KEY
47
 
        echo
48
 
        echo "We have following keys on the device: "
49
 
        echo $KEYS
50
 
        if [[ -z $KEYS || $KEYS == *No\ such* ]]; then
51
 
            echo "*no keys*"
52
 
            echo
53
 
            deploy_key
54
 
        else
55
 
            echo
56
 
            echo "The host key has been already deployed."
57
 
        fi
58
38
    fi
59
39
}
60
40
 
61
 
function deploy_key {
62
 
    echo "Deploy the host key to the device.."
63
 
    KEY=`cat $SSHIDENTITY.pub`
64
 
    adb_shell "mkdir -p /home/$USERNAME/.ssh"
65
 
    echo "..key folder created!"
66
 
    adb_shell "echo $KEY >> /home/$USERNAME/.ssh/authorized_keys2"
67
 
    echo "..key deployed!"
68
 
    adb_shell "chown -R $USERNAME:$USERNAME /home/$USERNAME/.ssh"
69
 
    adb_shell "chmod 0700 /home/$USERNAME/.ssh"
70
 
    adb_shell "chmod 0600 /home/$USERNAME/.ssh/authorized_keys2"
71
 
    echo "..permissions updated!"
72
 
}
73
 
 
74
41
function generate_key {
75
42
    echo "Generating host key.."
76
43
    if [[ -f $SSHIDENTITY ]]; then
82
49
 
83
50
#################
84
51
 
 
52
exec {lock_fd}>${CONFIGDIR}/.keylock || exit 1
 
53
flock -x -w 10 "$lock_fd" || { echo "ERROR: flock() failed." >&2; exit 1; }
 
54
 
85
55
if [[ -f $SSHIDENTITY ]]; then
86
56
  check_key
87
57
else
88
58
  generate_key
89
 
  deploy_key
90
59
fi