ufw-init-functions 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. #!/bin/sh
  2. #
  3. # ufw-init-functions: functions used by ufw-init and distribution initscripts
  4. #
  5. # Copyright 2008-2015 Canonical Ltd.
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License version 3,
  9. # as published by the Free Software Foundation.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. set -e
  20. PATH="/sbin:/bin:/usr/sbin:/usr/bin"
  21. for s in "${DATA_DIR}/etc/default/ufw" "${DATA_DIR}/etc/ufw/ufw.conf" ; do
  22. if [ -s "$s" ]; then
  23. . "$s"
  24. else
  25. echo "Could not find $s (aborting)"
  26. exit 1
  27. fi
  28. done
  29. RULES_PATH="${DATA_DIR}/etc/ufw"
  30. USER_PATH="${DATA_DIR}/etc/ufw"
  31. flush_builtins() {
  32. error=""
  33. execs="iptables"
  34. if ip6tables -L INPUT -n >/dev/null 2>&1; then
  35. execs="$execs ip6tables"
  36. fi
  37. for exe in $execs
  38. do
  39. $exe -F || error="yes"
  40. $exe -X || error="yes"
  41. $exe -P INPUT ACCEPT || error="yes"
  42. $exe -P OUTPUT ACCEPT || error="yes"
  43. $exe -P FORWARD ACCEPT || error="yes"
  44. # now handle the mangle table
  45. if $exe -t mangle -L -n >/dev/null 2>&1; then
  46. for i in INPUT OUTPUT FORWARD PREROUTING POSTROUTING ; do
  47. $exe -t mangle -F $i || error="yes"
  48. $exe -t mangle -P $i ACCEPT || error="yes"
  49. done
  50. fi
  51. done
  52. # now handle the nat table
  53. if iptables -t nat -L -n >/dev/null 2>&1; then
  54. for i in OUTPUT PREROUTING POSTROUTING ; do
  55. iptables -t nat -F $i || error="yes"
  56. iptables -t nat -P $i ACCEPT || error="yes"
  57. done
  58. fi
  59. if [ "$error" = "yes" ]; then
  60. return 1
  61. fi
  62. }
  63. chains_command() {
  64. flag="$1"
  65. type=""
  66. exe="iptables"
  67. if [ "$2" = "6" ]; then
  68. type="$2"
  69. exe="ip6tables"
  70. fi
  71. for c in ufw$type-logging-deny ufw$type-logging-allow ufw$type-not-local ufw$type-user-logging-input ufw$type-user-limit-accept ufw$type-user-limit ufw$type-skip-to-policy-input ufw$type-reject-input ufw$type-after-logging-input ufw$type-after-input ufw$type-user-input ufw$type-before-input ufw$type-before-logging-input ufw$type-skip-to-policy-forward ufw$type-reject-forward ufw$type-after-logging-forward ufw$type-after-forward ufw$type-user-logging-forward ufw$type-user-forward ufw$type-before-forward ufw$type-before-logging-forward ufw$type-track-forward ufw$type-track-output ufw$type-track-input ufw$type-skip-to-policy-output ufw$type-reject-output ufw$type-after-logging-output ufw$type-after-output ufw$type-user-logging-output ufw$type-user-output ufw$type-before-output ufw$type-before-logging-output; do
  72. if [ "$UFW_INIT_DEBUG" = "yes" ]; then
  73. echo "$exe $flag $c" >&2
  74. $exe $flag $c || true
  75. else
  76. $exe $flag $c 2>/dev/null || true
  77. fi
  78. done
  79. }
  80. delete_chains() {
  81. chains_command -F $1
  82. chains_command -Z $1
  83. # Delete the secondary chains to reduce clutter, but keep the primary ones
  84. # so that the primary chains don't leave the built-in chains just to come
  85. # back later in a different place. This means that some (empty) chains will
  86. # linger until the next boot after disabling ufw.
  87. for c in ufw$type-logging-deny ufw$type-logging-allow ufw$type-not-local ufw$type-user-logging-input ufw$type-user-logging-output ufw$type-user-logging-forward ufw$type-user-limit-accept ufw$type-user-limit ufw$type-user-input ufw$type-user-forward ufw$type-user-output ufw$type-skip-to-policy-input ufw$type-skip-to-policy-output ufw$type-skip-to-policy-forward ; do
  88. if [ "$UFW_INIT_DEBUG" = "yes" ]; then
  89. echo "$exe $flag $c" >&2
  90. $exe -X $c || true
  91. else
  92. $exe -X $c 2>/dev/null || true
  93. fi
  94. done
  95. }
  96. ufw_start() {
  97. out=""
  98. if [ "$ENABLED" = "yes" ] || [ "$ENABLED" = "YES" ]; then
  99. if iptables -L ufw-user-input -n >/dev/null 2>&1 ; then
  100. echo "Firewall already started, use 'force-reload'"
  101. return 0
  102. fi
  103. for m in $IPT_MODULES
  104. do
  105. modprobe $m || true
  106. done
  107. if [ "$MANAGE_BUILTINS" = "yes" ]; then
  108. flush_builtins
  109. fi
  110. if [ -x "$RULES_PATH/before.init" ]; then
  111. if ! "$RULES_PATH/before.init" start ; then
  112. error="yes"
  113. out="${out}\n'$RULES_PATH/before.init start' exited with error"
  114. fi
  115. fi
  116. execs="iptables"
  117. # IPv6 setup
  118. if [ "$IPV6" = "yes" ] || [ "$IPV6" = "YES" ]; then
  119. if ip6tables -L INPUT -n >/dev/null 2>&1; then
  120. execs="$execs ip6tables"
  121. else
  122. out="${out}\nProblem loading ipv6 (skipping)"
  123. fi
  124. else
  125. if ip6tables -L INPUT -n >/dev/null 2>&1; then
  126. # IPv6 support disabled but available in the kernel, so
  127. # default DROP and accept all on loopback
  128. delete_chains 6 || error="yes"
  129. printf "*filter\n"\
  130. ":INPUT DROP [0:0]\n"\
  131. ":FORWARD DROP [0:0]\n"\
  132. ":OUTPUT DROP [0:0]\n"\
  133. "-A INPUT -i lo -j ACCEPT\n"\
  134. "-A OUTPUT -o lo -j ACCEPT\n"\
  135. "COMMIT\n" | ip6tables-restore || error="yes"
  136. if [ "$error" = "yes" ]; then
  137. out="${out}\nProblem loading ipv6 (skipping)"
  138. fi
  139. fi
  140. fi
  141. for exe in $execs
  142. do
  143. type=""
  144. if [ "$exe" = "ip6tables" ]; then
  145. type="6"
  146. fi
  147. BEFORE_RULES="$RULES_PATH/before${type}.rules"
  148. AFTER_RULES="$RULES_PATH/after${type}.rules"
  149. USER_RULES="$USER_PATH/user${type}.rules"
  150. # set the default policy
  151. input_pol="$DEFAULT_INPUT_POLICY"
  152. if [ "$DEFAULT_INPUT_POLICY" = "REJECT" ]; then
  153. input_pol="DROP"
  154. fi
  155. output_pol="$DEFAULT_OUTPUT_POLICY"
  156. if [ "$DEFAULT_OUTPUT_POLICY" = "REJECT" ]; then
  157. output_pol="DROP"
  158. fi
  159. forward_pol="$DEFAULT_FORWARD_POLICY"
  160. if [ "$DEFAULT_FORWARD_POLICY" = "REJECT" ]; then
  161. forward_pol="DROP"
  162. fi
  163. printf "*filter\n"\
  164. "# builtin chains\n"\
  165. ":INPUT %s [0:0]\n"\
  166. ":FORWARD %s [0:0]\n"\
  167. ":OUTPUT %s [0:0]\n"\
  168. "COMMIT\n" $input_pol $forward_pol $output_pol | $exe-restore -n || error="yes"
  169. # flush the chains (if they exist)
  170. if $exe -L ufw${type}-before-logging-input -n >/dev/null 2>&1 ; then
  171. delete_chains $type || error="yes"
  172. else
  173. # setup all the primary chains
  174. printf "*filter\n"\
  175. "# primary chains\n"\
  176. ":ufw${type}-before-logging-input - [0:0]\n"\
  177. ":ufw${type}-before-logging-output - [0:0]\n"\
  178. ":ufw${type}-before-logging-forward - [0:0]\n"\
  179. ":ufw${type}-before-input - [0:0]\n"\
  180. ":ufw${type}-before-output - [0:0]\n"\
  181. ":ufw${type}-before-forward - [0:0]\n"\
  182. ":ufw${type}-after-input - [0:0]\n"\
  183. ":ufw${type}-after-output - [0:0]\n"\
  184. ":ufw${type}-after-forward - [0:0]\n"\
  185. ":ufw${type}-after-logging-input - [0:0]\n"\
  186. ":ufw${type}-after-logging-output - [0:0]\n"\
  187. ":ufw${type}-after-logging-forward - [0:0]\n"\
  188. ":ufw${type}-reject-input - [0:0]\n"\
  189. ":ufw${type}-reject-output - [0:0]\n"\
  190. ":ufw${type}-reject-forward - [0:0]\n"\
  191. ":ufw${type}-track-input - [0:0]\n"\
  192. ":ufw${type}-track-output - [0:0]\n"\
  193. ":ufw${type}-track-forward - [0:0]\n"\
  194. "\n"\
  195. "-A INPUT -j ufw${type}-before-logging-input\n"\
  196. "-A INPUT -j ufw${type}-before-input\n"\
  197. "-A INPUT -j ufw${type}-after-input\n"\
  198. "-A INPUT -j ufw${type}-after-logging-input\n"\
  199. "-A INPUT -j ufw${type}-reject-input\n"\
  200. "-A INPUT -j ufw${type}-track-input\n"\
  201. "\n"\
  202. "-A OUTPUT -j ufw${type}-before-logging-output\n"\
  203. "-A OUTPUT -j ufw${type}-before-output\n"\
  204. "-A OUTPUT -j ufw${type}-after-output\n"\
  205. "-A OUTPUT -j ufw${type}-after-logging-output\n"\
  206. "-A OUTPUT -j ufw${type}-reject-output\n"\
  207. "-A OUTPUT -j ufw${type}-track-output\n"\
  208. "\n"\
  209. "-A FORWARD -j ufw${type}-before-logging-forward\n"\
  210. "-A FORWARD -j ufw${type}-before-forward\n"\
  211. "-A FORWARD -j ufw${type}-after-forward\n"\
  212. "-A FORWARD -j ufw${type}-after-logging-forward\n"\
  213. "-A FORWARD -j ufw${type}-reject-forward\n"\
  214. "-A FORWARD -j ufw${type}-track-forward\n"\
  215. "COMMIT\n" | $exe-restore -n || error="yes"
  216. fi
  217. # add reject policy
  218. if [ "$DEFAULT_INPUT_POLICY" = "REJECT" ]; then
  219. printf "*filter\n"\
  220. "-A ufw${type}-reject-input -j REJECT\n"\
  221. "COMMIT\n" | $exe-restore -n || error="yes"
  222. fi
  223. if [ "$DEFAULT_OUTPUT_POLICY" = "REJECT" ]; then
  224. printf "*filter\n"\
  225. "-A ufw${type}-reject-output -j REJECT\n"\
  226. "COMMIT\n" | $exe-restore -n || error="yes"
  227. fi
  228. if [ "$DEFAULT_FORWARD_POLICY" = "REJECT" ]; then
  229. printf "*filter\n"\
  230. "-A ufw${type}-reject-forward -j REJECT\n"\
  231. "COMMIT\n" | $exe-restore -n || error="yes"
  232. fi
  233. # add tracking policy
  234. if [ "$DEFAULT_INPUT_POLICY" = "ACCEPT" ]; then
  235. printf "*filter\n"\
  236. "-A ufw${type}-track-input -p tcp -m conntrack --ctstate NEW -j ACCEPT\n"\
  237. "-A ufw${type}-track-input -p udp -m conntrack --ctstate NEW -j ACCEPT\n"\
  238. "COMMIT\n" | $exe-restore -n || error="yes"
  239. fi
  240. if [ "$DEFAULT_OUTPUT_POLICY" = "ACCEPT" ]; then
  241. printf "*filter\n"\
  242. "-A ufw${type}-track-output -p tcp -m conntrack --ctstate NEW -j ACCEPT\n"\
  243. "-A ufw${type}-track-output -p udp -m conntrack --ctstate NEW -j ACCEPT\n"\
  244. "COMMIT\n" | $exe-restore -n || error="yes"
  245. fi
  246. if [ "$DEFAULT_FORWARD_POLICY" = "ACCEPT" ]; then
  247. printf "*filter\n"\
  248. "-A ufw${type}-track-forward -p tcp -m conntrack --ctstate NEW -j ACCEPT\n"\
  249. "-A ufw${type}-track-forward -p udp -m conntrack --ctstate NEW -j ACCEPT\n"\
  250. "COMMIT\n" | $exe-restore -n || error="yes"
  251. fi
  252. # now setup the secondary 'logging-deny' chains
  253. if ! $exe -L ufw${type}-logging-deny -n >/dev/null 2>&1 ; then
  254. printf "*filter\n"\
  255. ":ufw${type}-logging-deny - [0:0]\n"\
  256. ":ufw${type}-logging-allow - [0:0]\n"\
  257. "COMMIT\n" | $exe-restore -n || error="yes"
  258. fi
  259. # now setup the secondary 'skip to policy' chains
  260. if ! $exe -L ufw${type}-skip-to-policy-input -n >/dev/null 2>&1 ; then
  261. printf "*filter\n"\
  262. ":ufw${type}-skip-to-policy-input - [0:0]\n"\
  263. ":ufw${type}-skip-to-policy-output - [0:0]\n"\
  264. ":ufw${type}-skip-to-policy-forward - [0:0]\n"\
  265. "-A ufw${type}-skip-to-policy-input -j %s\n"\
  266. "-A ufw${type}-skip-to-policy-output -j %s\n"\
  267. "-A ufw${type}-skip-to-policy-forward -j %s\n"\
  268. "COMMIT\n" $DEFAULT_INPUT_POLICY $DEFAULT_OUTPUT_POLICY $DEFAULT_FORWARD_POLICY | $exe-restore -n || error="yes"
  269. fi
  270. # now ip[6]tables-restore before*.rules. This resets the following
  271. # chains:
  272. # ufw-before-input
  273. # ufw-before-output
  274. # ufw-before-forward
  275. #
  276. # and sets the following:
  277. # ufw-not-local
  278. if [ -s "$BEFORE_RULES" ]; then
  279. if ! $exe-restore -n < "$BEFORE_RULES" ; then
  280. out="${out}\nProblem running '$BEFORE_RULES'"
  281. error="yes"
  282. fi
  283. else
  284. out="${out}\nCouldn't find '$BEFORE_RULES'"
  285. error="yes"
  286. fi
  287. # now ip[6]tables-restore after*.rules. This resets the following
  288. # chains:
  289. # ufw-after-input
  290. # ufw-after-output
  291. # ufw-after-forward
  292. if [ -s "$AFTER_RULES" ]; then
  293. if ! $exe-restore -n < "$AFTER_RULES" ; then
  294. out="${out}\nProblem running '$AFTER_RULES'"
  295. error="yes"
  296. fi
  297. else
  298. out="${out}\nCouldn't find '$AFTER_RULES'"
  299. error="yes"
  300. fi
  301. # user chains
  302. if [ -s "$USER_RULES" ]; then
  303. # setup the secondary 'user' chains
  304. if ! $exe -L ufw${type}-user-input -n >/dev/null 2>&1 ; then
  305. printf "*filter\n"\
  306. ":ufw${type}-user-input - [0:0]\n"\
  307. ":ufw${type}-user-output - [0:0]\n"\
  308. ":ufw${type}-user-forward - [0:0]\n"\
  309. ":ufw${type}-user-logging-input - [0:0]\n"\
  310. ":ufw${type}-user-logging-output - [0:0]\n"\
  311. ":ufw${type}-user-logging-forward - [0:0]\n"\
  312. ":ufw${type}-user-limit - [0:0]\n"\
  313. ":ufw${type}-user-limit-accept - [0:0]\n"\
  314. "COMMIT\n" | $exe-restore -n || error="yes"
  315. fi
  316. # now ip[6]tables-restore user*.rules. This resets the following
  317. # chains:
  318. # ufw-before-logging-input
  319. # ufw-before-logging-output
  320. # ufw-before-logging-forward
  321. # ufw-after-logging-input
  322. # ufw-after-logging-output
  323. # ufw-after-logging-forward
  324. # ufw-logging-deny
  325. # ufw-logging-allow
  326. # ufw-after-input
  327. # ufw-after-output
  328. # ufw-after-forward
  329. # ufw-user-limit
  330. # ufw-user-limit-accept
  331. if ! $exe-restore -n < "$USER_RULES" ; then
  332. out="${out}\nProblem running '$USER_RULES'"
  333. error="yes"
  334. fi
  335. # now hooks these into the primary chains
  336. printf "*filter\n"\
  337. "-A ufw${type}-before-input -j ufw${type}-user-input\n"\
  338. "-A ufw${type}-before-output -j ufw${type}-user-output\n"\
  339. "-A ufw${type}-before-forward -j ufw${type}-user-forward\n"\
  340. "COMMIT\n" | $exe-restore -n || error="yes"
  341. else
  342. out="${out}\nCouldn't find '$USER_RULES'"
  343. error="yes"
  344. fi
  345. done
  346. if [ ! -z "$IPT_SYSCTL" ] && [ -s "$IPT_SYSCTL" ]; then
  347. sysctl -e -q -p $IPT_SYSCTL || true
  348. fi
  349. if [ -x "$RULES_PATH/after.init" ]; then
  350. if ! "$RULES_PATH/after.init" start ; then
  351. error="yes"
  352. out="${out}\n'$RULES_PATH/after.init start' exited with error"
  353. fi
  354. fi
  355. if [ "$error" = "yes" ]; then
  356. /bin/echo -e "$out"
  357. return 1
  358. fi
  359. else
  360. out="Skip starting firewall: ufw (not enabled)"
  361. fi
  362. if [ ! -z "$out" ]; then
  363. /bin/echo -e "$out"
  364. fi
  365. }
  366. ufw_stop() {
  367. if [ "$1" != "--force" ] && [ "$ENABLED" != "yes" ] && [ "$ENABLED" != "YES" ]; then
  368. echo "Skip stopping firewall: ufw (not enabled)"
  369. return 0
  370. fi
  371. error=""
  372. if [ -x "$RULES_PATH/before.init" ]; then
  373. if ! "$RULES_PATH/before.init" stop ; then
  374. error="yes"
  375. out="${out}\n'$RULES_PATH/before.init stop' exited with error"
  376. fi
  377. fi
  378. # If we manage the builtins, just return
  379. if [ "$MANAGE_BUILTINS" = "yes" ]; then
  380. flush_builtins || error="yes"
  381. if [ -x "$RULES_PATH/after.init" ]; then
  382. "$RULES_PATH/after.init" stop || error="yes"
  383. fi
  384. if [ "$error" = "yes" ]; then
  385. return 1
  386. fi
  387. return 0
  388. fi
  389. execs="iptables"
  390. if ip6tables -L INPUT -n >/dev/null 2>&1; then
  391. execs="$execs ip6tables"
  392. fi
  393. for exe in $execs
  394. do
  395. type=""
  396. if [ "$exe" = "ip6tables" ]; then
  397. type="6"
  398. fi
  399. delete_chains $type || error="yes"
  400. $exe -P INPUT ACCEPT || error="yes"
  401. $exe -P OUTPUT ACCEPT || error="yes"
  402. $exe -P FORWARD ACCEPT || error="yes"
  403. done
  404. if [ -x "$RULES_PATH/after.init" ]; then
  405. if ! "$RULES_PATH/after.init" stop ; then
  406. error="yes"
  407. fi
  408. fi
  409. if [ "$error" = "yes" ]; then
  410. return 1
  411. fi
  412. return 0
  413. }
  414. ufw_reload() {
  415. if [ "$ENABLED" = "yes" ] || [ "$ENABLED" = "YES" ]; then
  416. if [ -x "$RULES_PATH/before.init" ]; then
  417. "$RULES_PATH/before.init" stop || return 1
  418. fi
  419. ufw_stop || return "$?"
  420. if [ -x "$RULES_PATH/after.init" ]; then
  421. "$RULES_PATH/after.init" stop || return 1
  422. fi
  423. if [ -x "$RULES_PATH/before.init" ]; then
  424. "$RULES_PATH/before.init" start || return 1
  425. fi
  426. ufw_start || return "$?"
  427. if [ -x "$RULES_PATH/after.init" ]; then
  428. "$RULES_PATH/after.init" start || return 1
  429. fi
  430. else
  431. echo "Skipping $1 (not enabled)"
  432. fi
  433. return 0
  434. }
  435. ufw_status() {
  436. err=""
  437. iptables -L ufw-user-input -n >/dev/null 2>&1 || {
  438. echo "Firewall is not running"
  439. return 3
  440. }
  441. if [ "$IPV6" = "yes" ] || [ "$IPV6" = "YES" ]; then
  442. ip6tables -L ufw6-user-input -n >/dev/null 2>&1 || {
  443. # unknown state: ipv4 ok, but ipv6 isn't
  444. echo "Firewall in inconsistent state (IPv6 enabled but not running)"
  445. return 4
  446. }
  447. fi
  448. echo "Firewall is running"
  449. return 0
  450. }