summaryrefslogtreecommitdiff
path: root/completions/ssh
blob: ebd7424a879497d30dae4bf6ae17d18840010b27 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# ssh(1) completion                                        -*- shell-script -*-

_ssh_ciphers()
{
    COMPREPLY+=( $( compgen -W '3des-cbc aes128-cbc aes192-cbc aes256-cbc
        aes128-ctr aes192-ctr aes256-ctr arcfour128 arcfour256 arcfour
        blowfish-cbc cast128-cbc' -- "$cur" ) )
}

_ssh_macs()
{
    COMPREPLY+=( $( compgen -W 'hmac-md5 hmac-sha1 umac-64@openssh.com
        hmac-ripemd160 hmac-sha1-96 hmac-md5-96' -- "$cur" ) )
}

_ssh_options()
{
    compopt -o nospace
    COMPREPLY=( $( compgen -S = -W 'AddressFamily BatchMode BindAddress
        ChallengeResponseAuthentication CheckHostIP Cipher Ciphers
        ClearAllForwardings Compression CompressionLevel ConnectionAttempts
        ConnectTimeout ControlMaster ControlPath ControlPersist DynamicForward
        EnableSSHKeysign EscapeChar ExitOnForwardFailure ForwardAgent
        ForwardX11 ForwardX11Timeout ForwardX11Trusted GatewayPorts
        GlobalKnownHostsFile GSSAPIAuthentication GSSAPIClientIdentity
        GSSAPIDelegateCredentials GSSAPIKeyExchange GSSAPIRenewalForcesRekey
        GSSAPIServerIdentity GSSAPITrustDns HashKnownHosts Host
        HostbasedAuthentication HostKeyAlgorithms HostKeyAlias HostName
        IdentityFile IdentitiesOnly IPQoS KbdInteractiveDevices KexAlgorithms
        LocalCommand LocalForward LogLevel MACs
        NoHostAuthenticationForLocalhost NumberOfPasswordPrompts
        PasswordAuthentication PermitLocalCommand PKCS11Provider Port
        PreferredAuthentications Protocol ProxyCommand PubkeyAuthentication
        RekeyLimit RemoteForward RequestTTY RhostsRSAAuthentication
        RSAAuthentication SendEnv ServerAliveCountMax ServerAliveInterval
        SmartcardDevice StrictHostKeyChecking TCPKeepAlive Tunnel TunnelDevice
        UsePrivilegedPort User UserKnownHostsFile VerifyHostKeyDNS
        VisualHostKey XAuthLocation' -- "$cur" ) )
}

# Complete a ssh suboption (like ForwardAgent=y<tab>)
# Only one parameter: the string to complete including the equal sign.
# Not all suboptions are completed.
# Doesn't handle comma-separated lists.
_ssh_suboption()
{
    # Split into subopt and subval
    local prev=${1%%=*} cur=${1#*=}

    case $prev in
        BatchMode|ChallengeResponseAuthentication|CheckHostIP|\
        ClearAllForwardings|ControlPersist|Compression|EnableSSHKeysign|\
        ExitOnForwardFailure|ForwardAgent|ForwardX11|ForwardX11Trusted|\
        GatewayPorts|GSSAPIAuthentication|GSSAPIKeyExchange|\
        GSSAPIDelegateCredentials|GSSAPIRenewalForcesRekey|GSSAPITrustDns|\
        HashKnownHosts|HostbasedAuthentication|IdentitiesOnly|\
        KbdInteractiveAuthentication|KbdInteractiveDevices|\
        NoHostAuthenticationForLocalhost|PasswordAuthentication|\
        PubkeyAuthentication|RhostsRSAAuthentication|RSAAuthentication|\
        StrictHostKeyChecking|TCPKeepAlive|UsePrivilegedPort|\
        VerifyHostKeyDNS|VisualHostKey)
            COMPREPLY=( $( compgen -W 'yes no' -- "$cur" ) )
            ;;
        AddressFamily)
            COMPREPLY=( $( compgen -W 'any inet inet6' -- "$cur" ) )
            ;;
        BindAddress)
            _ip_addresses
            ;;
        Cipher)
            COMPREPLY=( $( compgen -W 'blowfish des 3des' -- "$cur" ) )
            ;;
        IPQoS)
            COMPREPLY=( $( compgen -W 'af1{1..4} af2{2..3} af3{1..3} af4{1..3}
                cs{0..7} ef lowdelay throughput reliability' -- "$cur" ) )
            ;;
        Protocol)
            COMPREPLY=( $( compgen -W '1 2 1,2 2,1' -- "$cur" ) )
            ;;
        RequestTTY)
            COMPREPLY=( $( compgen -W 'no yes force auto' -- "$cur" ) )
            ;;
        Tunnel)
            COMPREPLY=( $( compgen -W 'yes no point-to-point ethernet' \
                    -- "$cur" ) )
            ;;
        PreferredAuthentications)
            COMPREPLY=( $( compgen -W 'gssapi-with-mic host-based publickey
                keyboard-interactive password' -- "$cur" ) )
            ;;
        MACs)
            _ssh_macs
            ;;
        Ciphers)
            _ssh_ciphers
            ;;
    esac
    return 0
}

# Try to complete -o SubOptions=
#
# Returns 0 if the completion was handled or non-zero otherwise.
_ssh_suboption_check()
{
    # Get prev and cur words without splitting on =
    local cureq=`_get_cword :=` preveq=`_get_pword :=`
    if [[ $cureq == *=* && $preveq == -o ]]; then
        _ssh_suboption $cureq
        return $?
    fi
    return 1
}

_ssh()
{
    local cur prev words cword
    _init_completion -n : || return

    local configfile
    local -a config

    _ssh_suboption_check && return 0

    case $prev in
        -F|-i|-S)
            _filedir
            return 0
            ;;
        -c)
            _ssh_ciphers
            return 0
            ;;
        -m)
            _ssh_macs
            return 0
            ;;
        -l)
            COMPREPLY=( $( compgen -u -- "$cur" ) )
            return 0
            ;;
        -O)
            COMPREPLY=( $( compgen -W 'check forward exit stop' -- "$cur" ) )
            return 0
            ;;
        -o)
            _ssh_options
            return 0
            ;;
        -w)
            _available_interfaces
            return 0
            ;;
        -b)
            _ip_addresses
            return 0
            ;;
        -D|-e|-I|-L|-p|-R|-W)
            return 0
            ;;
    esac

    if [[ "$cur" == -F* ]]; then
        cur=${cur#-F}
        _filedir
        # Prefix completions with '-F'
        COMPREPLY=( "${COMPREPLY[@]/#/-F}" )
        cur=-F$cur  # Restore cur
    elif [[ "$cur" == -* ]]; then
        COMPREPLY=( $( compgen -W '$( _parse_usage "$1" )' -- "$cur" ) )
    else
        # Search COMP_WORDS for '-F configfile' or '-Fconfigfile' argument
        set -- "${words[@]}"
        while [[ $# -gt 0 ]]; do
            if [[ $1 == -F* ]]; then
                if [[ ${#1} -gt 2 ]]; then
                    configfile="$(dequote "${1:2}")"
                else
                    shift
                    [[ $1 ]] && configfile="$(dequote "$1")"
                fi
                break
            fi
            shift
        done
        _known_hosts_real -a -F "$configfile" "$cur"
        if [[ $cword -ne 1 ]]; then
            compopt -o filenames
            COMPREPLY+=( $( compgen -c -- "$cur" ) )
        fi
    fi

    return 0
} &&
shopt -u hostcomplete && complete -F _ssh ssh slogin autossh

# sftp(1) completion
#
_sftp()
{
    local cur prev words cword
    _init_completion || return

    local configfile

    _ssh_suboption_check && return 0

    case $prev in
        -b|-F|-i)
            _filedir
            return 0
            ;;
        -o)
            _ssh_options
            return 0
            ;;
        -c)
            _ssh_ciphers
            return 0
            ;;
        -B|-D|-P|-R|-S|-s)
            return 0
            ;;
    esac

    if [[ "$cur" == -F* ]]; then
        cur=${cur#-F}
        _filedir
        # Prefix completions with '-F'
        COMPREPLY=( "${COMPREPLY[@]/#/-F}" )
        cur=-F$cur  # Restore cur
    elif [[ "$cur" == -* ]]; then
        COMPREPLY=( $( compgen -W '$( _parse_usage "$1" )' -- "$cur" ) )
    else
        # Search COMP_WORDS for '-F configfile' argument
        set -- "${words[@]}"
        while [[ $# -gt 0 ]]; do
            if [[ $1 == -F* ]]; then
                if [[ ${#1} -gt 2 ]]; then
                    configfile="$(dequote "${1:2}")"
                else
                    shift
                    [[ $1 ]] && configfile="$(dequote "$1")"
                fi
                break
            fi
            shift
        done
        _known_hosts_real -a -F "$configfile" "$cur"
    fi

    return 0
} &&
shopt -u hostcomplete && complete -F _sftp sftp

# things we want to backslash escape in scp paths
_scp_path_esc='[][(){}<>",:;^&!$=?`|\\'"'"'[:space:]]'

# Complete remote files with ssh.  If the first arg is -d, complete on dirs
# only.  Returns paths escaped with three backslashes.
_scp_remote_files()
{
    local IFS=$'\n'

    # remove backslash escape from the first colon
    cur=${cur/\\:/:}

    local userhost=${cur%%?(\\):*}
    local path=${cur#*:}

    # unescape (3 backslashes to 1 for chars we escaped)
    path=$( sed -e 's/\\\\\\\('$_scp_path_esc'\)/\\\1/g' <<<"$path" )

    # default to home dir of specified user on remote host
    if [[ -z $path ]]; then
        path=$(ssh -o 'Batchmode yes' $userhost pwd 2>/dev/null)
    fi

    local files
    if [[ $1 == -d ]]; then
        # escape problematic characters; remove non-dirs
        files=$( ssh -o 'Batchmode yes' $userhost \
            command ls -aF1dL "$path*" 2>/dev/null | \
            sed -e 's/'$_scp_path_esc'/\\\\\\&/g' -e '/[^\/]$/d' )
    else
        # escape problematic characters; remove executables, aliases, pipes
        # and sockets; add space at end of file names
        files=$( ssh -o 'Batchmode yes' $userhost \
            command ls -aF1dL "$path*" 2>/dev/null | \
            sed -e 's/'$_scp_path_esc'/\\\\\\&/g' -e 's/[*@|=]$//g' \
            -e 's/[^\/]$/& /g' )
    fi
    COMPREPLY+=( $files )
}

# This approach is used instead of _filedir to get a space appended
# after local file/dir completions, and -o nospace retained for others.
# If first arg is -d, complete on directory names only.  The next arg is
# an optional prefix to add to returned completions.
_scp_local_files()
{
    local IFS=$'\n'

    local dirsonly=false
    if [[ $1 == -d ]]; then
        dirsonly=true
        shift
    fi

    if $dirsonly ; then
        COMPREPLY+=( $( command ls -aF1dL $cur* 2>/dev/null | \
            sed -e "s/$_scp_path_esc/\\\\&/g" -e '/[^\/]$/d' -e "s/^/$1/") )
    else
        COMPREPLY+=( $( command ls -aF1dL $cur* 2>/dev/null | \
            sed -e "s/$_scp_path_esc/\\\\&/g" -e 's/[*@|=]$//g' \
            -e 's/[^\/]$/& /g' -e "s/^/$1/") )
    fi
}

# scp(1) completion
#
_scp()
{
    local cur prev words cword
    _init_completion -n : || return

    local configfile prefix

    _ssh_suboption_check && {
        COMPREPLY=( "${COMPREPLY[@]/%/ }" )
        return 0
    }

    case $prev in
        -l|-P)
            return 0
            ;;
        -F|-i|-S)
            _filedir
            compopt +o nospace
            return 0
            ;;
        -c)
            _ssh_ciphers
            COMPREPLY=( "${COMPREPLY[@]/%/ }" )
            return 0
            ;;
        -o)
            _ssh_options
            return 0
            ;;
    esac

    _expand || return 0

    case $cur in
        !(*:*)/*|[.~]*) ;; # looks like a path
        *:*) _scp_remote_files ; return 0 ;;
    esac

    if [[ "$cur" == -F* ]]; then
        cur=${cur#-F}
        prefix=-F
    else
        # Search COMP_WORDS for '-F configfile' or '-Fconfigfile' argument
        set -- "${words[@]}"
        while [[ $# -gt 0 ]]; do
            if [[ $1 == -F* ]]; then
                if [[ ${#1} -gt 2 ]]; then
                    configfile="$(dequote "${1:2}")"
                else
                    shift
                    [[ $1 ]] && configfile="$(dequote "$1")"
                fi
                break
            fi
            shift
        done

        case $cur in
            -*)
                COMPREPLY=( $( compgen -W '$( _parse_usage "${words[0]}" )' \
                    -- "$cur" ) )
                COMPREPLY=( "${COMPREPLY[@]/%/ }" )
                return 0
                ;;
            */*|[.~]*)
                # not a known host, pass through
                ;;
            *)
                _known_hosts_real -c -a -F "$configfile" "$cur"
                ;;
        esac
    fi

    _scp_local_files "$prefix"

    return 0
} &&
complete -F _scp -o nospace scp

# ex: ts=4 sw=4 et filetype=sh