_ufw 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #compdef ufw
  2. #autoload
  3. typeset -A opt_args
  4. function _ufw_delete_rules {
  5. if ufw status &> /dev/null ; then
  6. ufw status numbered \
  7. | perl -n -e'/\[ +(\d+)\] +([^ ].+)/ && print "\"$1\[$2\]\" "'
  8. fi
  9. }
  10. function _ufw_app_profiles {
  11. grep -rhoP "(?<=\[)[^\]]+" /etc/ufw/applications.d/ \
  12. | awk '{ print "\""$0"\""}' \
  13. | tr '\n' ' '
  14. }
  15. local -a _1st_arguments
  16. _1st_arguments=(
  17. 'allow:add allow rule'
  18. 'app:Application profile commands'
  19. 'default:set default policy'
  20. 'delete:delete RULE'
  21. 'deny:add deny rule'
  22. 'disable:disables the firewall'
  23. 'enable:enables the firewall'
  24. 'insert:insert RULE at NUM'
  25. 'limit:add limit rule'
  26. 'logging:set logging to LEVEL'
  27. 'reject:add reject rule'
  28. 'reload:reloads firewall'
  29. 'reset:reset firewall'
  30. 'show:show firewall report'
  31. 'status:show firewall status'
  32. 'version:display version information'
  33. )
  34. local context state line curcontext="$curcontext"
  35. _arguments -C \
  36. '(--dry-run)--dry-run[dry run]' \
  37. '1:: :->cmds' \
  38. '2:: :->subcmds' \
  39. '3:: :->subsubcmds' \
  40. && return 0
  41. local rules
  42. case "$state" in
  43. (cmds)
  44. _describe -t commands "ufw commands" _1st_arguments
  45. return 0
  46. ;;
  47. (subcmds)
  48. case "$line[1]" in
  49. (app)
  50. _values 'app' \
  51. 'list[list application profiles]' \
  52. 'info[show information on PROFILE]' \
  53. 'update[update PROFILE]' \
  54. 'default[set default application policy]' \
  55. && ret=0
  56. ;;
  57. (status)
  58. _values 'status' \
  59. 'numbered[show firewall status as numbered list of RULES]' \
  60. 'verbose[show verbose firewall status]' \
  61. && ret=0
  62. ;;
  63. (logging)
  64. _values 'logging' \
  65. 'on' 'off' 'low' 'medium' 'high' 'full' \
  66. && ret=0
  67. ;;
  68. (default)
  69. _values 'default' \
  70. 'allow' 'deny' 'reject' \
  71. && ret=0
  72. ;;
  73. (show)
  74. _values 'show' \
  75. 'raw' 'builtins' 'before-rules' 'user-rules' 'after-rules' 'logging-rules' 'listening' 'added' \
  76. && ret=0
  77. ;;
  78. (delete)
  79. rules="$(_ufw_delete_rules)"
  80. if [[ -n "$rules" ]] ; then
  81. _values 'delete' \
  82. ${(Q)${(z)"$(_ufw_delete_rules)"}} \
  83. && ret=0
  84. fi
  85. ;;
  86. esac
  87. ;;
  88. (subsubcmds)
  89. case "$line[1]" in
  90. (app)
  91. case "$line[2]" in
  92. (info|update)
  93. _values 'profiles' \
  94. ${(Q)${(z)"$(_ufw_app_profiles)"}} \
  95. && ret=0
  96. ;;
  97. esac
  98. ;;
  99. (default)
  100. _values 'default-direction' \
  101. 'incoming' 'outgoing' \
  102. && ret=0
  103. ;;
  104. esac
  105. esac
  106. return