summaryrefslogtreecommitdiff
path: root/src/get-dyndns-name
blob: 7ef8c1b63cfbeb3c916e392218fc47755e37783f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/bin/sh
set -e

DEFAULT_AUTH_TYPE=ed25519

in_group()
{
    local g
    for g in $(groups)
    do
        [ "$g" = "$1" ] && return
    done
    false
}

ssh_keytag_to_path_fragment()
{
    case "$1" in
        ssh-dss) echo dsa ;;
        ecdsa-sha2-nistp256) echo ecdsa ;;
        ssh-rsa|ssh-ed25519) echo ${1#ssh-} ;;
        *) return 1 ;;
    esac
}

path_fragment_to_ssh_keytag()
{
    case "$1" in
        ssh-dss|ecdsa-sha2-nistp256|ssh-rsa|ssh-ed25519) echo $1;;
        dss|rsa|ed25519) echo ssh-$1 ;;
        dsa) echo ssh-dss ;;
        ecdsa) echo ecdsa-sha2-nistp256 ;;
        *) return 1 ;;
    esac
}

get_dyndns_domain()
{
        fragment=$(ssh_keytag_to_path_fragment "$1") || return

        host_keyfile=/etc/ssh/ssh_host_${fragment}_key
        user_keyfile=$HOME/.ssh/id_${fragment}

        set -- -q dyndns@cryptonomic.net

        if [ -r "$host_keyfile" ]
        then
                set -- ssh -i "$host_keyfile" "$@"

        elif in_group sudo
        then
                set -- sudo ssh -i "$host_keyfile" "$@"

        elif [ -r "$user_keyfile" ]
        then
                set -- ssh -i "$user_keyfile" "$@"

        else
                set -- ssh "$@"
        fi

        "$@"
}

AUTH_TYPE=${1:-$DEFAULT_AUTH_TYPE}
AUTH_KEYTAG=$(path_fragment_to_ssh_keytag "$AUTH_TYPE")
get_dyndns_domain "$AUTH_KEYTAG"