summaryrefslogtreecommitdiffstats
path: root/completions/ssh
blob: 907c0390fc93a8792a196325479f066545c4f337 (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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
# ssh(1) completion                                        -*- shell-script -*-

_ssh_queries()
{
    COMPREPLY+=($(compgen -W \
        "cipher cipher-auth help mac kex key key-cert key-plain key-sig
         protocol-version compression sig
         ciphers macs kexalgorithms pubkeyacceptedkeytypes
         hostkeyalgorithms hostbasedkeytypes hostbasedacceptedkeytypes" \
        -- "${cur,,}"))
}

_ssh_query()
{
    ${1:-ssh} -Q $2 2>/dev/null
}

_ssh_ciphers()
{
    local ciphers='$(_ssh_query "$1" cipher)'
    [[ $ciphers ]] || ciphers="3des-cbc aes128-cbc aes192-cbc aes256-cbc
        aes128-ctr aes192-ctr aes256-ctr arcfour128 arcfour256 arcfour
        blowfish-cbc cast128-cbc"
    COMPREPLY+=($(compgen -W "$ciphers" -- "$cur"))
}

_ssh_macs()
{
    local macs='$(_ssh_query "$1" mac)'
    [[ $macs ]] || macs="hmac-md5 hmac-sha1 umac-64@openssh.com hmac-ripemd160
        hmac-sha1-96 hmac-md5-96"
    COMPREPLY+=($(compgen -W "$macs" -- "$cur"))
}

_ssh_options()
{
    local opts=(
        AddKeysToAgent AddressFamily BatchMode BindAddress CanonicalDomains
        CanonicalizeFallbackLocal CanonicalizeHostname CanonicalizeMaxDots
        CanonicalizePermittedCNAMEs CASignatureAlgorithms CertificateFile
        ChallengeResponseAuthentication CheckHostIP Ciphers ClearAllForwardings
        Compression ConnectionAttempts ConnectTimeout ControlMaster ControlPath
        ControlPersist DynamicForward EnableSSHKeysign EscapeChar
        ExitOnForwardFailure FingerprintHash ForwardAgent ForwardX11
        ForwardX11Timeout ForwardX11Trusted GatewayPorts GlobalKnownHostsFile
        GSSAPIAuthentication GSSAPIClientIdentity GSSAPIDelegateCredentials
        GSSAPIKeyExchange GSSAPIRenewalForcesRekey GSSAPIServerIdentity
        GSSAPITrustDns HashKnownHosts Host HostbasedAuthentication
        HostbasedKeyTypes HostKeyAlgorithms HostKeyAlias HostName
        IdentitiesOnly IdentityAgent IdentityFile IgnoreUnknown Include IPQoS
        KbdInteractiveAuthentication KbdInteractiveDevices KexAlgorithms
        LocalCommand LocalForward LogLevel MACs
        NoHostAuthenticationForLocalhost NumberOfPasswordPrompts
        PasswordAuthentication PermitLocalCommand PKCS11Provider Port
        PreferredAuthentications ProxyCommand ProxyJump ProxyUseFdpass
        PubkeyAcceptedKeyTypes PubkeyAuthentication RekeyLimit RemoteCommand
        RemoteForward RequestTTY RevokedHostKeys SendEnv ServerAliveCountMax
        ServerAliveInterval SmartcardDevice StreamLocalBindMask
        StreamLocalBindUnlink StrictHostKeyChecking SyslogFacility TCPKeepAlive
        Tunnel TunnelDevice UpdateHostKeys UsePrivilegedPort User
        UserKnownHostsFile VerifyHostKeyDNS VisualHostKey XAuthLocation)
    local protocols=$(_ssh_query "$1" protocol-version)
    if [[ -z $protocols || $protocols == *1* ]]; then
        opts+=(Cipher CompressionLevel Protocol RhostsRSAAuthentication
            RSAAuthentication)
    fi

    compopt -o nospace
    local IFS=$' \t\n' reset=$(shopt -p nocasematch)
    shopt -s nocasematch
    local option
    COMPREPLY=($(for option in "${opts[@]}"; do
        [[ $option == "$cur"* ]] && printf '%s=\n' "$option"
    done))
    $reset
}

# Complete a ssh suboption (like ForwardAgent=y<tab>)
# Two parameters: the string to complete including the equal sign, and
# the ssh executable to invoke (optional).
# 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 | canonicaldomains | canonicalizefallbacklocal | \
            challengeresponseauthentication | checkhostip | \
            clearallforwardings | controlpersist | compression | enablesshkeysign | \
            exitonforwardfailure | forwardagent | forwardx11 | forwardx11trusted | \
            gatewayports | gssapiauthentication | gssapikeyexchange | \
            gssapidelegatecredentials | gssapirenewalforcesrekey | gssapitrustdns | \
            hashknownhosts | hostbasedauthentication | identitiesonly | \
            kbdinteractiveauthentication | kbdinteractivedevices | \
            nohostauthenticationforlocalhost | passwordauthentication | permitlocalcommand | \
            proxyusefdpass | pubkeyauthentication | rhostsrsaauthentication | \
            rsaauthentication | streamlocalbindunlink | \
            tcpkeepalive | useprivilegedport | visualhostkey)
            COMPREPLY=($(compgen -W 'yes no' -- "$cur"))
            ;;
        addkeystoagent)
            COMPREPLY=($(compgen -W 'yes ask confirm no' -- "$cur"))
            ;;
        addressfamily)
            COMPREPLY=($(compgen -W 'any inet inet6' -- "$cur"))
            ;;
        bindaddress)
            _ip_addresses
            ;;
        canonicalizehostname)
            COMPREPLY=($(compgen -W 'yes no always' -- "$cur"))
            ;;
        identityfile)
            _ssh_identityfile
            ;;
        *file | identityagent | include | controlpath | revokedhostkeys | xauthlocation)
            _filedir
            ;;
        casignaturealgorithms)
            COMPREPLY=($(compgen -W '$(_ssh_query "$2" sig)' -- "$cur"))
            ;;
        cipher)
            COMPREPLY=($(compgen -W 'blowfish des 3des' -- "$cur"))
            ;;
        ciphers)
            _ssh_ciphers "$2"
            ;;
        controlmaster)
            COMPREPLY=($(compgen -W 'yes ask auto autoask no' -- "$cur"))
            ;;
        compressionlevel)
            COMPREPLY=($(compgen -W '{1..9}' -- "$cur"))
            ;;
        fingerprinthash)
            COMPREPLY=($(compgen -W 'md5 sha256' -- "$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"))
            ;;
        hostbasedkeytypes | hostkeyalgorithms)
            COMPREPLY=($(compgen -W '$(_ssh_query "$2" key)' -- "$cur"))
            ;;
        kexalgorithms)
            COMPREPLY=($(compgen -W '$(_ssh_query "$2" kex)' -- "$cur"))
            ;;
        loglevel)
            COMPREPLY=($(compgen -W 'QUIET FATAL ERROR INFO VERBOSE DEBUG{,1,2,3}' -- "$cur"))
            ;;
        macs)
            _ssh_macs "$2"
            ;;
        pkcs11provider)
            _filedir so
            ;;
        preferredauthentications)
            COMPREPLY=($(compgen -W 'gssapi-with-mic host-based publickey
                keyboard-interactive password' -- "$cur"))
            ;;
        protocol)
            local protocols=($(_ssh_query "$2" protocol-version))
            [[ $protocols ]] || protocols=(1 2)
            if ((${#protocols[@]} > 1)); then
                COMPREPLY=($(compgen -W '${protocols[@]}' -- "$cur"))
            fi
            ;;
        proxyjump)
            _known_hosts_real -a ${configfile:+-F "$configfile"} -- "$cur"
            ;;
        proxycommand | remotecommand | localcommand)
            COMPREPLY=($(compgen -c -- "$cur"))
            ;;
        pubkeyacceptedkeytypes)
            COMPREPLY=($(compgen -W '$(_ssh_query "$2" key)' -- "$cur"))
            ;;
        requesttty)
            COMPREPLY=($(compgen -W 'no yes force auto' -- "$cur"))
            ;;
        stricthostkeychecking)
            COMPREPLY=($(compgen -W 'accept-new ask no off' -- "$cur"))
            ;;
        syslogfacility)
            COMPREPLY=($(compgen -W 'DAEMON USER AUTH LOCAL{0..7}' -- "$cur"))
            ;;
        tunnel)
            COMPREPLY=($(compgen -W 'yes no point-to-point ethernet' \
                -- "$cur"))
            ;;
        updatehostkeys | verifyhostkeydns)
            COMPREPLY=($(compgen -W 'yes no ask' -- "$cur"))
            ;;
    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 "$1"
        return $?
    fi
    return 1
}

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

# With $1 set, look for public key files, else private
# shellcheck disable=SC2120
_ssh_identityfile()
{
    [[ -z $cur && -d ~/.ssh ]] && cur=~/.ssh/id
    _filedir
    if ((${#COMPREPLY[@]} > 0)); then
        COMPREPLY=($(compgen -W '${COMPREPLY[@]}' \
            -X "${1:+!}*.pub" -- "$cur"))
    fi
}

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

    local configfile
    _ssh_configfile

    _ssh_suboption_check "$1" && return

    local ipvx

    case $prev in
        -*b)
            _ip_addresses
            return
            ;;
        -*c)
            _ssh_ciphers "$1"
            return
            ;;
        -*[DeLpRW])
            return
            ;;
        -*[EFS])
            _filedir
            return
            ;;
        -*i)
            _ssh_identityfile
            return
            ;;
        -*I)
            _filedir so
            return
            ;;
        -*J)
            _known_hosts_real -a ${configfile:+-F "$configfile"} -- "$cur"
            return
            ;;
        -*l)
            COMPREPLY=($(compgen -u -- "$cur"))
            return
            ;;
        -*m)
            _ssh_macs "$1"
            return
            ;;
        -*O)
            COMPREPLY=($(compgen -W 'check forward cancel exit stop' -- "$cur"))
            return
            ;;
        -*o)
            _ssh_options "$1"
            return
            ;;
        -*Q)
            _ssh_queries "$1"
            return
            ;;
        -*w)
            _available_interfaces
            return
            ;;
        -*4*)
            ipvx=-4
            ;;
        -*6*)
            ipvx=-6
            ;;
    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
        _known_hosts_real ${ipvx-} -a ${configfile:+-F "$configfile"} -- "$cur"

        local args
        _count_args
        if ((args > 1)); then
            compopt -o filenames
            COMPREPLY+=($(compgen -c -- "$cur"))
        fi
    fi
} &&
    shopt -u hostcomplete && complete -F _ssh ssh slogin autossh sidedoor

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

    local configfile
    _ssh_configfile

    _ssh_suboption_check && return

    local ipvx

    case $prev in
        -*[BDlPRs])
            return
            ;;
        -*[bF])
            _filedir
            return
            ;;
        -*i)
            _ssh_identityfile
            return
            ;;
        -*c)
            _ssh_ciphers
            return
            ;;
        -*J)
            _known_hosts_real -a ${configfile:+-F "$configfile"} -- "$cur"
            return
            ;;
        -*o)
            _ssh_options
            return
            ;;
        -*S)
            compopt -o filenames
            COMPREPLY=($(compgen -c -- "$cur"))
            return
            ;;
        -*4*)
            ipvx=-4
            ;;
        -*6*)
            ipvx=-6
            ;;
    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
        _known_hosts_real ${ipvx-} -a ${configfile:+-F "$configfile"} -- "$cur"
    fi
} &&
    shopt -u hostcomplete && complete -F _sftp sftp

# things we want to backslash escape in scp paths
# shellcheck disable=SC2089
_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.
# shellcheck disable=SC2120
_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)
    # shellcheck disable=SC2090
    path=$(command 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
        # shellcheck disable=SC2090
        files=$(ssh -o 'Batchmode yes' $userhost \
            command ls -aF1dL "$path*" 2>/dev/null |
            command 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
        # shellcheck disable=SC2090
        files=$(ssh -o 'Batchmode yes' $userhost \
            command ls -aF1dL "$path*" 2>/dev/null |
            command 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 |
            command sed -e "s/$_scp_path_esc/\\\\&/g" -e '/[^\/]$/d' -e "s/^/${1-}/"))
    else
        COMPREPLY+=($(command ls -aF1dL $cur* 2>/dev/null |
            command 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
    _ssh_configfile

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

    local ipvx

    case $prev in
        -*c)
            _ssh_ciphers
            COMPREPLY=("${COMPREPLY[@]/%/ }")
            return
            ;;
        -*F)
            _filedir
            compopt +o nospace
            return
            ;;
        -*i)
            _ssh_identityfile
            compopt +o nospace
            return
            ;;
        -*J)
            _known_hosts_real -a ${configfile:+-F "$configfile"} -- "$cur"
            return
            ;;
        -*[lP])
            return
            ;;
        -*o)
            _ssh_options
            return
            ;;
        -*S)
            compopt +o nospace -o filenames
            COMPREPLY=($(compgen -c -- "$cur"))
            return
            ;;
        -*4*)
            ipvx=-4
            ;;
        -*6*)
            ipvx=-6
            ;;
    esac

    _expand || return

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

    local prefix

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

    _scp_local_files "${prefix-}"
} &&
    complete -F _scp -o nospace scp

# ex: filetype=sh