2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2014 Michael Tremer #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
20 ###############################################################################
21 # network(8) completion
23 _network_find_on_cmdline
() {
24 local word subcommand c
=0
25 while [ ${c} -lt ${cword} ]; do
27 for subcommand
in ${1}; do
28 if [ "${subcommand}" = "${word}" ]; then
37 _network_complete_hooks
() {
40 COMPREPLY
=( $
(compgen
-W "$(network raw list-hooks "${type}")" -- "${cur}") )
43 _network_complete_ports
() {
44 COMPREPLY
=( $
(compgen
-W "$(network raw list-ports)" -- "${cur}") )
47 _network_complete_zones
() {
48 COMPREPLY
=( $
(compgen
-W "$(network raw list-zones)" -- "${cur}") )
54 local commands
="set reset"
55 local cmd
="$(_network_find_on_cmdline "${commands}")"
56 if [[ -z "${cmd}" ]]; then
57 COMPREPLY
=( $
(compgen
-W "${commands}" -- "${cur}") )
62 _network_description
() {
65 local commands
="edit show"
66 local cmd
="$(_network_find_on_cmdline "${commands}")"
67 if [[ -z "${cmd}" ]]; then
68 COMPREPLY
=( $
(compgen
-W "${commands}" -- "${cur}") )
75 local commands
="list $(network raw list-devices)"
76 local cmd
="$(_network_find_on_cmdline "${commands}")"
77 if [[ -z "${cmd}" ]]; then
78 COMPREPLY
=( $
(compgen
-W "${commands}" -- "${cur}") )
82 local args
="${words[@]:1}"
88 _network_device_subcommand
${args}
93 _network_device_subcommand
() {
96 local commands
="discover identify monitor status unlock ussd"
97 local cmd
="$(_network_find_on_cmdline "${commands}")"
98 if [[ -z "${cmd}" ]]; then
99 COMPREPLY
=( $
(compgen
-W "${commands}" -- "${cur}") )
116 local commands
="edit reload restart show start stop subnet"
117 local cmd
="$(_network_find_on_cmdline "${commands}")"
118 if [[ -z "${cmd}" ]]; then
119 COMPREPLY
=( $
(compgen
-W "${commands}" -- "${cur}") )
123 local args
="${words[@]:1}"
126 _network_dhcpd_subnet
"${proto}" ${args}
131 _network_dhcpd_subnet
() {
137 local commands
="new remove show $(network raw list-dhcpd-subnets "${proto}")"
138 local cmd
="$(_network_find_on_cmdline "${commands}")"
139 if [[ -z "${cmd}" ]]; then
140 COMPREPLY
=( $
(compgen
-W "${commands}" -- "${cur}") )
144 local subnet
="${words[0]}"
145 local args
="${words[@]:1}"
154 _network_dhcpd_subnet_subcommand
"${proto}" "${subnet}" ${args}
159 _network_dhcpd_subnet_subcommand() {
166 local commands="edit options range show
"
167 local cmd="$
(_network_find_on_cmdline
"${commands}")"
168 if [[ -z "${cmd}" ]]; then
169 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
173 local args="${words[@]:1}"
179 _network_dhcpd_subnet_subcommand_options "${proto}" "${subnet}" ${args}
182 _network_dhcpd_subnet_subcommand_range
"${proto}" "${subnet}" ${args}
187 _network_dhcpd_subnet_subcommand_options() {
193 for option in $(network raw list-dhcpd-subnet-options "${proto}"); do
194 options="${options} ${option}="
197 COMPREPLY=( $(compgen -W "${options}" -- "${cur}") )
200 _network_dhcpd_subnet_subcommand_range() {
207 local commands="new remove
"
208 local cmd="$
(_network_find_on_cmdline
"${commands}")"
209 if [[ -z "${cmd}" ]]; then
210 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
216 COMPREPLY=( $(compgen -W "--end= --start=" -- "${cur}") )
219 COMPREPLY=( $(compgen -W "$
(network raw list-dhcpd-ranges-of-subnet
"${proto}" "${subnet}")" \
225 _network_dns_server() {
228 local commands="add list remove update
"
229 local cmd="$
(_network_find_on_cmdline
"${commands}")"
230 if [[ -z "${cmd}" ]]; then
231 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
237 COMPREPLY=( $(compgen -W "$
(network raw list-dns-servers
)" \
246 local commands="new destroy $
(network raw list-ports
)"
247 local cmd="$
(_network_find_on_cmdline
"${commands}")"
248 if [[ -z "${cmd}" ]]; then
249 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
253 local args="${words[@]:1}"
256 _network_complete_hooks "port
"
259 _network_complete_ports
262 local args="${words[@]:1}"
263 _network_port_subcommand ${args}
268 _network_port_subcommand() {
271 local commands="color create description down edit identify remove status up
"
272 local cmd="$
(_network_find_on_cmdline
"${commands}")"
273 if [[ -z "${cmd}" ]]; then
274 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
278 local args="${words[@]:1}"
281 _network_color ${args}
284 _network_description ${args}
293 local commands="static
"
294 local cmd="$
(_network_find_on_cmdline
"${commands}")"
295 if [[ -z "${cmd}" ]]; then
296 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
302 local args="${words[@]}"
303 _network_route_static ${args}
309 _network_route_static() {
312 local commands="add list remove reload
"
313 local cmd="$
(_network_find_on_cmdline
"${commands}")"
314 if [[ -z "${cmd}" ]]; then
315 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
321 if [[ "${cur}" = -* ]]; then
322 COMPREPLY=( $(compgen -W "--blackhole --gateway= --mtu= \
323 --prohibit --unreachable" -- "${cur}") )
327 # TODO auto-complete options like --protocol here
328 COMPREPLY=( $(compgen -W "--protocol=" -- "${cur}") )
333 _network_settings() {
337 for key in $(network raw list-settings); do
338 keys="${keys} ${key}="
340 COMPREPLY=( $(compgen -W "${keys}" -- "${cur}") )
346 local commands="ipsec security-policies
"
347 local cmd="$
(_network_find_on_cmdline
"${commands}")"
348 if [[ -z "${cmd}" ]]; then
349 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
354 local args="${words[@]:1}"
357 _network_vpn_ipsec ${args}
360 _network_vpn_security_policies ${args}
365 _network_vpn_ipsec() {
368 local commands="connection
"
369 local cmd="$
(_network_find_on_cmdline
"${commands}")"
370 if [[ -z "${cmd}" ]]; then
371 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
376 local args="${words[@]:1}"
379 _network_vpn_ipsec_connection ${args}
384 _network_vpn_ipsec_connection() {
387 local commands="destroy new $
(network raw list-ipsec-connections
)"
388 local cmd="$
(_network_find_on_cmdline
"${commands}")"
389 if [[ -z "${cmd}" ]]; then
390 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
395 local args="${words[@]:1}"
404 if network raw ipsec-connection-exists ${cmd}; then
405 _network_vpn_ipsec_connection_subcommands ${cmd} ${args}
411 _network_vpn_ipsec_connection_subcommands() {
412 local connection=${1}
416 local commands="authentication color description down inactivity-timeout
local mode peer remote security-policy show up zone
"
417 local cmd="$
(_network_find_on_cmdline
"${commands}")"
418 if [[ -z "${cmd}" ]]; then
419 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
424 local args="${words[@]:1}"
427 _network_vpn_ipsec_connection_subcommands_authentication ${connection} ${args}
430 _network_color ${args}
433 _network_description ${args}
436 _network_vpn_ipsec_connection_subcommands_local_remote ${connection} "local" ${args}
439 _network_vpn_ipsec_connection_subcommands_mode ${args}
442 _network_vpn_ipsec_connection_subcommands_local_remote ${connection} "remote
" ${args}
445 _network_vpn_ipsec_connection_subcommands_security_policy ${args}
448 _network_vpn_ipsec_connection_subcommands_zone "${connection}" ${args}
453 _network_vpn_ipsec_connection_subcommands_authentication() {
454 local connection=${1}
458 local commands="mode pre-shared-key
"
459 local cmd="$
(_network_find_on_cmdline
"${commands}")"
460 if [[ -z "${cmd}" ]]; then
461 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
466 local args="${words[@]:1}"
469 _network_vpn_ipsec_connection_subcommands_authentication_mode ${connection} ${args}
474 _network_vpn_ipsec_connection_subcommands_local_remote() {
475 local connection=${1}
480 local commands="id prefix
"
481 local cmd="$
(_network_find_on_cmdline
"${commands}")"
482 if [[ -z "${cmd}" ]]; then
483 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
488 local args="${words[@]:1}"
491 _network_vpn_ipsec_connection_subcommands_prefix "${connection}" "${type}" ${args}
496 _network_vpn_ipsec_connection_subcommands_prefix
() {
500 _network_vpn_ipsec_connection_subcommands_authentication_mode
() {
501 local connection
=${1}
506 local cmd
="$(_network_find_on_cmdline "${commands}")"
507 if [[ -z "${cmd}" ]]; then
508 COMPREPLY
=( $
(compgen
-W "${commands}" -- "${cur}") )
513 _network_vpn_ipsec_connection_subcommands_mode
() {
516 local commands
="transport tunnel"
517 local cmd
="$(_network_find_on_cmdline "${commands}")"
518 if [[ -z "${cmd}" ]]; then
519 COMPREPLY
=( $
(compgen
-W "${commands}" -- "${cur}") )
524 _network_vpn_ipsec_connection_subcommands_security_policy
() {
527 local commands
="$(network raw list-vpn-security-policies-all)"
528 local cmd
="$(_network_find_on_cmdline "${commands}")"
529 if [[ -z "${cmd}" ]]; then
530 COMPREPLY
=( $
(compgen
-W "${commands}" -- "${cur}") )
535 _network_vpn_ipsec_connection_subcommands_zone
() {
536 local connection
="${1}"
541 # XXX TODO find zones that can be attached here
544 _network_vpn_security_policies
() {
547 local commands
="destroy new $(network raw list-vpn-security-policies-all)"
548 local cmd
="$(_network_find_on_cmdline "${commands}")"
549 if [[ -z "${cmd}" ]]; then
550 COMPREPLY
=( $
(compgen
-W "${commands}" -- "${cur}") )
555 local args
="${words[@]:1}"
564 if network raw vpn-security-policy-exists
${cmd}; then
565 _network_vpn_security_policies_subcommands
${cmd} ${args}
571 _network_vpn_security_policies_subcommands
() {
576 local commands
="ciphers compression group-types integrities key-exchange lifetime pfs pseudo-random-functions show"
577 local cmd
="$(_network_find_on_cmdline "${commands}")"
578 if [[ -z "${cmd}" ]]; then
579 COMPREPLY
=( $
(compgen
-W "${commands}" -- "${cur}") )
584 local args
="${words[@]:1}"
587 _network_vpn_security_policies_subcommands_ciphers
${policy} ${args}
590 _network_vpn_security_policies_subcommands_compression
${policy} ${args}
593 _network_vpn_security_policies_subcommands_group_types
${policy} ${args}
596 _network_vpn_security_policies_subcommands_integrities
${policy} ${args}
598 pseudo-random-functions
)
599 _network_vpn_security_policies_subcommands_pseudo_random_functions
${policy} ${args}
602 _network_vpn_security_policies_subcommands_key_exchange
${policy} ${args}
605 _network_vpn_security_policies_subcommands_pfs
${policy} ${args}
610 _network_vpn_security_policies_subcommands_ciphers
() {
614 _network_vpn_security_policies_subcommands_compression
() {
618 _network_vpn_security_policies_subcommands_group_types
() {
622 _network_vpn_security_policies_subcommands_integrities
() {
626 _network_vpn_security_policies_subcommands_pseudo_random_functions
() {
630 _network_vpn_security_policies_subcommands_key_exchange
() {
634 _network_vpn_security_policies_subcommands_pfs
() {
641 local commands
="new destroy $(network raw list-zones)"
642 local cmd
="$(_network_find_on_cmdline "${commands}")"
643 if [[ -z "${cmd}" ]]; then
644 COMPREPLY
=( $
(compgen
-W "${commands}" -- "${cur}") )
649 local args
="${words[@]:1}"
652 _network_zone_new
${args}
655 _network_complete_zones
659 local args
="${words[@]:1}"
660 _network_zone_subcommand
"${zone}" ${args}
665 _network_zone_new
() {
667 local cmd
=${words[@]:0:1}
669 # Suggest useful zone names
670 if [[ -z "${cmd}" ]]; then
671 local commands
="$(network raw list-next-free-zones)"
672 COMPREPLY
=( $
(compgen
-W "${commands}" -- "${cur}") )
674 # If a valid zone name was entered, we can move on
675 elif network raw zone-name-is-valid
${cmd}; then
676 local args
="${words[@]:1}"
677 _network_complete_hooks zone
${args}
683 _network_zone_subcommand
() {
689 local commands
="color config description disable down edit enable identify port rename status up"
690 local cmd
="$(_network_find_on_cmdline "${commands}")"
691 if [[ -z "${cmd}" ]]; then
692 COMPREPLY
=( $
(compgen
-W "${commands}" -- "${cur}") )
696 local args
="${words[@]:1}"
699 _network_zone_subcommand_config
"${zone}" ${args}
702 _network_zone_subcommand_port
"${zone}" ${args}
705 _network_color
${args}
708 _network_description
${args}
713 _network_zone_subcommand_config
() {
719 local commands
="destroy list new $(network raw list-zone-config-hids ${zone})"
721 local cmd
="$(_network_find_on_cmdline "${commands}")"
722 if [[ -z "${cmd}" ]]; then
723 COMPREPLY
=( $
(compgen
-W "${commands}" -- "${cur}") )
727 local args
="${words[@]:1}"
730 _network_complete_hooks
"config"
733 _network_zone_subcommand_config_destroy
${zone} ${args}
736 if network raw zone-config-id-is-valid
${zone} ${cmd} || network raw zone-config-hid-is-valid ${zone} ${cmd}; then
737 _network_zone_subcommand_config_subcommand
${zone} ${args}
743 _network_zone_subcommand_config_subcommand
() {
749 local commands
="edit"
751 local cmd
="$(_network_find_on_cmdline "${commands}")"
752 if [[ -z "${cmd}" ]]; then
753 COMPREPLY
=( $
(compgen
-W "${commands}" -- "${cur}") )
758 _network_zone_subcommand_config_destroy
() {
764 local commands
="$(network raw list-zone-config-ids ${zone})"
766 local cmd
="$(_network_find_on_cmdline "${commands}")"
767 if [[ -z "${cmd}" ]]; then
768 COMPREPLY
=( $
(compgen
-W "${commands}" -- "${cur}") )
773 _network_zone_subcommand_port
() {
779 local commands
="attach detach $(network raw list-ports-of-zone "${zone}")"
780 local cmd
="$(_network_find_on_cmdline "${commands}")"
781 if [[ -z "${cmd}" ]]; then
782 COMPREPLY
=( $
(compgen
-W "${commands}" -- "${cur}") )
788 COMPREPLY
=( $
(compgen
-W "$(network raw list-free-ports "${zone}")" \
792 COMPREPLY
=( $
(compgen
-W "$(network raw list-ports-of-zone "${zone}")" \
797 local args
="${words[@]:1}"
798 _network_zone_subcommand_port_subcommand
"${zone}" "${port}" ${args}
803 _network_zone_subcommand_port_subcommand() {
810 local commands="edit
"
811 local cmd="$
(_network_find_on_cmdline
"${commands}")"
812 if [[ -z "${cmd}" ]]; then
813 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
819 # TODO auto-complete the zone-port hook
825 local cur prev words cword
826 _init_completion || return
829 for (( i=1; i < ${#words[@]} - 1; i++ )); do
830 [[ "${words[i]}" = -* ]] && continue
831 cmd="${words[i]}" && break
834 if [[ -z "${cmd}" ]]; then
837 COMPREPLY=( $(compgen -W "--debug" -- "${cur}") )
840 COMPREPLY=( $(compgen -W "device dhcpv4 dhcpv6 dns-server \
841 help hostname port
reset route settings status vpn zone
" \
849 local args="${words[@]:i+1}"
852 _network_device ${args}
855 _network_dhcpd "${cmd/dhcpv/ipv}" ${args}
858 _network_dns_server ${args}
861 _network_port ${args}
864 _network_route ${args}
867 _network_settings ${args}
870 # start, stop and status optionally take a zone
871 _network_complete_zones
877 _network_zone ${args}
880 } && complete -F _network network