diff options
| author | David M. Syzdek <syzdek@bindlebinaries.com> | 2019-07-29 14:45:54 -0800 |
|---|---|---|
| committer | David M. Syzdek <syzdek@bindlebinaries.com> | 2019-07-29 14:50:38 -0800 |
| commit | 4ca93e73238c31ec0e0496709ff83c98cf32562f (patch) | |
| tree | 8d7b13984103f8f0dceaf38f4aab92045726163c | |
| parent | a8229766e35dd21d75bf694baeb6c6b4cb62ce4b (diff) | |
| download | bbfw-4ca93e73238c31ec0e0496709ff83c98cf32562f.tar.gz bbfw-4ca93e73238c31ec0e0496709ff83c98cf32562f.tar.bz2 bbfw-4ca93e73238c31ec0e0496709ff83c98cf32562f.tar.xz bbfw-4ca93e73238c31ec0e0496709ff83c98cf32562f.zip | |
Adding ability to change order in which files are processed
| -rw-r--r-- | src/bbfw.conf-example.in | 47 | ||||
| -rwxr-xr-x | src/bbfw.in | 222 |
2 files changed, 216 insertions, 53 deletions
diff --git a/src/bbfw.conf-example.in b/src/bbfw.conf-example.in index e8a62fa..24a8955 100644 --- a/src/bbfw.conf-example.in +++ b/src/bbfw.conf-example.in | |||
| @@ -45,4 +45,51 @@ | |||
| 45 | # verbose = yes | 45 | # verbose = yes |
| 46 | 46 | ||
| 47 | 47 | ||
| 48 | # Options which specify order of executions or loading of rules use the | ||
| 49 | # following keywords: | ||
| 50 | # | ||
| 51 | # global - files within %{firewall_dir}/global.d/ | ||
| 52 | # apps - files within %{firewall_dir}/apps.d/%{app}/ | ||
| 53 | # cluster - files within %{firewall_dir}/cluster.d/ | ||
| 54 | # local - files within %{firewall_dir}/ | ||
| 55 | # | ||
| 56 | # Types of files are always processed in the following order: | ||
| 57 | # | ||
| 58 | # pre-hooks (start and stop) | ||
| 59 | # ipsets load (start only) | ||
| 60 | # iptables load (start only) | ||
| 61 | # iptables flush (stop only / not user definable) | ||
| 62 | # ipsets flush (stop only / not user definable) | ||
| 63 | # post-hooks (start and stop) | ||
| 64 | # | ||
| 65 | |||
| 66 | |||
| 67 | # The `order_prehook` option defines the order in which pre-hooks are | ||
| 68 | # executed. To modify the order in which pre-hooks are executed, uncomment | ||
| 69 | # the line below and modify the order of the key words. See above for | ||
| 70 | # keyword descriptions. | ||
| 71 | # order_prehook = global apps cluster local | ||
| 72 | |||
| 73 | |||
| 74 | # The `order_posthook` option defines the order in which post-hooks are | ||
| 75 | # executed. To modify the order in which post-hooks are executed, uncomment | ||
| 76 | # the line below and modify the order of the key words. See above for | ||
| 77 | # keyword descriptions. | ||
| 78 | # order_posthook = global apps cluster local | ||
| 79 | |||
| 80 | |||
| 81 | # The `order_ipsets` option defines the order in which ipsets are | ||
| 82 | # loaded. To modify the order in which ipsets are loaded, uncomment | ||
| 83 | # the line below and modify the order of the key words. See above for | ||
| 84 | # keyword descriptions. | ||
| 85 | # order_ipsets = global apps cluster local | ||
| 86 | |||
| 87 | |||
| 88 | # The `order_iptables` option defines the order in which iptables | ||
| 89 | # and ip6tables are loaded. To modify the order in which rules are loaded, | ||
| 90 | # uncomment the line below and modify the order of the key words. See above | ||
| 91 | # for keyword descriptions. | ||
| 92 | # order_iptables = apps local cluster global | ||
| 93 | |||
| 94 | |||
| 48 | # end of configuration | 95 | # end of configuration |
diff --git a/src/bbfw.in b/src/bbfw.in index 00c45b9..40fdcb8 100755 --- a/src/bbfw.in +++ b/src/bbfw.in | |||
| @@ -82,6 +82,10 @@ FW_PREFIX=@sysconfdir@/firewall.d/ | |||
| 82 | FW_CONF=@sysconfdir@/bbfw.conf | 82 | FW_CONF=@sysconfdir@/bbfw.conf |
| 83 | FW_QUIET=no | 83 | FW_QUIET=no |
| 84 | FW_VERBOSE=no | 84 | FW_VERBOSE=no |
| 85 | FW_ORDER_PRE="global apps cluster local" | ||
| 86 | FW_ORDER_POST="global apps cluster local" | ||
| 87 | FW_ORDER_IPSETS="global apps cluster local" | ||
| 88 | FW_ORDER_IPTABLES="apps local cluster global" | ||
| 85 | 89 | ||
| 86 | 90 | ||
| 87 | ########### | 91 | ########### |
| @@ -218,70 +222,159 @@ load() | |||
| 218 | # Function to start the network: | 222 | # Function to start the network: |
| 219 | start() { | 223 | start() { |
| 220 | # run pre-hooks | 224 | # run pre-hooks |
| 221 | run_hook start ${FW_PREFIX}/global.d/pre-hook "running global pre-hook start ..." | 225 | for LEVEL in ${FW_ORDER_PRE};do |
| 222 | for FILE in $( find_appfile pre-hook );do | 226 | case "${LEVEL}" in |
| 223 | run_hook start ${FILE} "running app pre-hook start (App: $(name_appfile ${FILE})) ..." | 227 | global) |
| 228 | run_hook start ${FW_PREFIX}/global.d/pre-hook "running global pre-hook start ..." | ||
| 229 | ;; | ||
| 230 | |||
| 231 | apps) | ||
| 232 | for FILE in $( find_appfile pre-hook );do | ||
| 233 | run_hook start ${FILE} "running app pre-hook start (App: $(name_appfile ${FILE})) ..." | ||
| 234 | done | ||
| 235 | ;; | ||
| 236 | |||
| 237 | cluster) | ||
| 238 | run_hook start ${FW_PREFIX}/cluster.d/pre-hook "running cluster pre-hook start ..." | ||
| 239 | ;; | ||
| 240 | |||
| 241 | local) | ||
| 242 | run_hook start ${FW_PREFIX}/pre-hook "running local pre-hook start ..." | ||
| 243 | ;; | ||
| 244 | |||
| 245 | *) | ||
| 246 | echo "Unknown pre-hook order \"${LEVEL}\"" | ||
| 247 | ;; | ||
| 248 | esac | ||
| 224 | done | 249 | done |
| 225 | run_hook start ${FW_PREFIX}/cluster.d/pre-hook "running cluster pre-hook start ..." | ||
| 226 | run_hook start ${FW_PREFIX}/pre-hook "running local pre-hook start ..." | ||
| 227 | 250 | ||
| 228 | # create IP sets | 251 | # create IP sets |
| 229 | load ipset ${FW_PREFIX}/global.d/ipsets "adding global IP sets" | 252 | for LEVEL in ${FW_ORDER_IPSETS};do |
| 230 | for FILE in $( find_appfile ipsets );do | 253 | case "${LEVEL}" in |
| 231 | load ipset ${FILE} "adding IP sets (App: $(name_appfile ${FILE})) ..." | 254 | global) |
| 255 | load ipset ${FW_PREFIX}/global.d/ipsets "adding global IP sets" | ||
| 256 | ;; | ||
| 257 | |||
| 258 | apps) | ||
| 259 | for FILE in $( find_appfile ipsets );do | ||
| 260 | load ipset ${FILE} "adding IP sets (App: $(name_appfile ${FILE})) ..." | ||
| 261 | done | ||
| 262 | ;; | ||
| 263 | |||
| 264 | cluster) | ||
| 265 | load ipset ${FW_PREFIX}/cluster.d/ipsets "adding cluster IP sets" | ||
| 266 | ;; | ||
| 267 | |||
| 268 | local) | ||
| 269 | load ipset ${FW_PREFIX}/ipsets "adding local IP sets" | ||
| 270 | ;; | ||
| 271 | |||
| 272 | *) | ||
| 273 | echo "Unknown ipsets order \"${LEVEL}\"" | ||
| 274 | ;; | ||
| 275 | esac | ||
| 232 | done | 276 | done |
| 233 | load ipset ${FW_PREFIX}/cluster.d/ipsets "adding cluster IP sets" | ||
| 234 | load ipset ${FW_PREFIX}/ipsets "adding local IP sets" | ||
| 235 | 277 | ||
| 236 | # Application rules | 278 | # loads iptables/ip6tables |
| 237 | for FILE in $( find_appfile rules4 );do | 279 | for LEVEL in ${FW_ORDER_IPTABLES};do |
| 238 | load iptables ${FILE} "adding IPv4 rules (App: $(name_appfile ${FILE})) ..." | 280 | case "${LEVEL}" in |
| 281 | apps) | ||
| 282 | for FILE in $( find_appfile rules4 );do | ||
| 283 | load iptables ${FILE} "adding IPv4 rules (App: $(name_appfile ${FILE})) ..." | ||
| 284 | done | ||
| 285 | for FILE in $( find_appfile rules6 );do | ||
| 286 | load ip6tables ${FILE} "adding IPv6 rules (App: $(name_appfile ${FILE})) ..." | ||
| 287 | done | ||
| 288 | for FILE in $( find_appfile rules );do | ||
| 289 | load iptables ${FILE} "adding IPv4/IPv6 rules (App: $(name_appfile ${FILE})) ..." | ||
| 290 | load ip6tables ${FILE} | ||
| 291 | done | ||
| 292 | ;; | ||
| 293 | |||
| 294 | local) | ||
| 295 | load iptables ${FW_PREFIX}/rules4 "adding IPv4 local rules" | ||
| 296 | load ip6tables ${FW_PREFIX}/rules6 "adding IPv6 local rules" | ||
| 297 | load iptables ${FW_PREFIX}/rules "adding IPv4/IPv6 local rules" | ||
| 298 | load ip6tables ${FW_PREFIX}/rules | ||
| 299 | ;; | ||
| 300 | |||
| 301 | cluster) | ||
| 302 | load iptables ${FW_PREFIX}/cluster.d/rules4 "adding IPv4 cluster rules" | ||
| 303 | load ip6tables ${FW_PREFIX}/cluster.d/rules6 "adding IPv6 cluster rules" | ||
| 304 | load iptables ${FW_PREFIX}/cluster.d/rules "adding IPv4/IPv6 cluster rules" | ||
| 305 | load ip6tables ${FW_PREFIX}/cluster.d/rules | ||
| 306 | ;; | ||
| 307 | |||
| 308 | global) | ||
| 309 | load iptables ${FW_PREFIX}/global.d/rules4 "adding IPv4 global rules" | ||
| 310 | load ip6tables ${FW_PREFIX}/global.d/rules6 "adding IPv6 global rules" | ||
| 311 | load iptables ${FW_PREFIX}/global.d/rules "adding IPv4/IPv6 global rules" | ||
| 312 | load ip6tables ${FW_PREFIX}/global.d/rules | ||
| 313 | ;; | ||
| 314 | |||
| 315 | *) | ||
| 316 | echo "Unknown iptables order \"${LEVEL}\"" | ||
| 317 | ;; | ||
| 318 | esac | ||
| 239 | done | 319 | done |
| 240 | for FILE in $( find_appfile rules6 );do | ||
| 241 | load ip6tables ${FILE} "adding IPv6 rules (App: $(name_appfile ${FILE})) ..." | ||
| 242 | done | ||
| 243 | for FILE in $( find_appfile rules );do | ||
| 244 | load iptables ${FILE} "adding IPv4/IPv6 rules (App: $(name_appfile ${FILE})) ..." | ||
| 245 | load ip6tables ${FILE} | ||
| 246 | done | ||
| 247 | |||
| 248 | # apply host specific firewall rules | ||
| 249 | load iptables ${FW_PREFIX}/rules4 "adding IPv4 local rules" | ||
| 250 | load ip6tables ${FW_PREFIX}/rules6 "adding IPv6 local rules" | ||
| 251 | load iptables ${FW_PREFIX}/rules "adding IPv4/IPv6 local rules" | ||
| 252 | load ip6tables ${FW_PREFIX}/rules | ||
| 253 | |||
| 254 | # apply cluster specific firewall rules | ||
| 255 | load iptables ${FW_PREFIX}/cluster.d/rules4 "adding IPv4 cluster rules" | ||
| 256 | load ip6tables ${FW_PREFIX}/cluster.d/rules6 "adding IPv6 cluster rules" | ||
| 257 | load iptables ${FW_PREFIX}/cluster.d/rules "adding IPv4/IPv6 cluster rules" | ||
| 258 | load ip6tables ${FW_PREFIX}/cluster.d/rules | ||
| 259 | |||
| 260 | # apply company policy for firewall rules | ||
| 261 | load iptables ${FW_PREFIX}/global.d/rules4 "adding IPv4 global rules" | ||
| 262 | load ip6tables ${FW_PREFIX}/global.d/rules6 "adding IPv6 global rules" | ||
| 263 | load iptables ${FW_PREFIX}/global.d/rules "adding IPv4/IPv6 global rules" | ||
| 264 | load ip6tables ${FW_PREFIX}/global.d/rules | ||
| 265 | 320 | ||
| 266 | # run post-hooks | 321 | # run post-hooks |
| 267 | run_hook start ${FW_PREFIX}/global.d/post-hook "running global post-hook start ..." | 322 | for LEVEL in ${FW_ORDER_POST};do |
| 268 | for FILE in $( find_appfile post-hook );do | 323 | case "${LEVEL}" in |
| 269 | run_hook start ${FILE} "running app post-hook start (App: $(name_appfile ${FILE})) ..." | 324 | global) |
| 325 | run_hook start ${FW_PREFIX}/global.d/post-hook "running global post-hook start ..." | ||
| 326 | ;; | ||
| 327 | |||
| 328 | apps) | ||
| 329 | for FILE in $( find_appfile post-hook );do | ||
| 330 | run_hook start ${FILE} "running app post-hook start (App: $(name_appfile ${FILE})) ..." | ||
| 331 | done | ||
| 332 | ;; | ||
| 333 | |||
| 334 | cluster) | ||
| 335 | run_hook start ${FW_PREFIX}/cluster.d/post-hook "running cluster post-hook start ..." | ||
| 336 | ;; | ||
| 337 | |||
| 338 | local) | ||
| 339 | run_hook start ${FW_PREFIX}/post-hook "running local post-hook start ..." | ||
| 340 | ;; | ||
| 341 | |||
| 342 | *) | ||
| 343 | echo "Unknown post-hook order \"${LEVEL}\"" | ||
| 344 | ;; | ||
| 345 | esac | ||
| 270 | done | 346 | done |
| 271 | run_hook start ${FW_PREFIX}/cluster.d/post-hook "running cluster post-hook start ..." | ||
| 272 | run_hook start ${FW_PREFIX}/post-hook "running local post-hook start ..." | ||
| 273 | } | 347 | } |
| 274 | 348 | ||
| 275 | 349 | ||
| 276 | # Function to stop the network: | 350 | # Function to stop the network: |
| 277 | stop() { | 351 | stop() { |
| 278 | # run pre-hooks | 352 | # run pre-hooks |
| 279 | run_hook stop ${FW_PREFIX}/global.d/pre-hook "running global pre-hook stop ..." | 353 | for LEVEL in ${FW_ORDER_PRE};do |
| 280 | for FILE in $( find_appfile pre-hook );do | 354 | case "${LEVEL}" in |
| 281 | run_hook stop ${FILE} "running app pre-hook stop (App: $(name_appfile ${FILE})) ..." | 355 | global) |
| 356 | run_hook stop ${FW_PREFIX}/global.d/pre-hook "running global pre-hook stop ..." | ||
| 357 | ;; | ||
| 358 | |||
| 359 | apps) | ||
| 360 | for FILE in $( find_appfile pre-hook );do | ||
| 361 | run_hook stop ${FILE} "running app pre-hook stop (App: $(name_appfile ${FILE})) ..." | ||
| 362 | done | ||
| 363 | ;; | ||
| 364 | |||
| 365 | cluster) | ||
| 366 | run_hook stop ${FW_PREFIX}/cluster.d/pre-hook "running cluster pre-hook stop ..." | ||
| 367 | ;; | ||
| 368 | |||
| 369 | local) | ||
| 370 | run_hook stop ${FW_PREFIX}/pre-hook "running local pre-hook stop ..." | ||
| 371 | ;; | ||
| 372 | |||
| 373 | *) | ||
| 374 | echo "Unknown pre-hook order \"${LEVEL}\"" | ||
| 375 | ;; | ||
| 376 | esac | ||
| 282 | done | 377 | done |
| 283 | run_hook stop ${FW_PREFIX}/cluster.d/pre-hook "running cluster pre-hook stop ..." | ||
| 284 | run_hook stop ${FW_PREFIX}/pre-hook "running local pre-hook stop ..." | ||
| 285 | 378 | ||
| 286 | # flushes iptables | 379 | # flushes iptables |
| 287 | for TABLE in $(iptables-save |grep '^*' |sed -e 's/^*//g' |sort);do | 380 | for TABLE in $(iptables-save |grep '^*' |sed -e 's/^*//g' |sort);do |
| @@ -323,12 +416,31 @@ stop() { | |||
| 323 | done | 416 | done |
| 324 | 417 | ||
| 325 | # run post-hooks | 418 | # run post-hooks |
| 326 | run_hook stop ${FW_PREFIX}/global.d/post-hook "running global post-hook stop ..." | 419 | for LEVEL in ${FW_ORDER_POST};do |
| 327 | for FILE in $( find_appfile post-hook );do | 420 | case "${LEVEL}" in |
| 328 | run_hook stop ${FILE} "running app post-hook stop (App: $(name_appfile ${FILE})) ..." | 421 | global) |
| 422 | run_hook stop ${FW_PREFIX}/global.d/post-hook "running global post-hook stop ..." | ||
| 423 | ;; | ||
| 424 | |||
| 425 | apps) | ||
| 426 | for FILE in $( find_appfile post-hook );do | ||
| 427 | run_hook stop ${FILE} "running app post-hook stop (App: $(name_appfile ${FILE})) ..." | ||
| 428 | done | ||
| 429 | ;; | ||
| 430 | |||
| 431 | cluster) | ||
| 432 | run_hook stop ${FW_PREFIX}/cluster.d/post-hook "running cluster post-hook stop ..." | ||
| 433 | ;; | ||
| 434 | |||
| 435 | local) | ||
| 436 | run_hook stop ${FW_PREFIX}/post-hook "running local post-hook stop ..." | ||
| 437 | ;; | ||
| 438 | |||
| 439 | *) | ||
| 440 | echo "Unknown post-hook order \"${LEVEL}\"" | ||
| 441 | ;; | ||
| 442 | esac | ||
| 329 | done | 443 | done |
| 330 | run_hook stop ${FW_PREFIX}/cluster.d/post-hook "running cluster post-hook stop ..." | ||
| 331 | run_hook stop ${FW_PREFIX}/post-hook "running local post-hook stop ..." | ||
| 332 | } | 444 | } |
| 333 | 445 | ||
| 334 | 446 | ||
| @@ -340,6 +452,10 @@ stop() { | |||
| 340 | FW_PREFIX="$( config_option firewall_dir "${FW_PREFIX}" )" | 452 | FW_PREFIX="$( config_option firewall_dir "${FW_PREFIX}" )" |
| 341 | FW_QUIET="$( config_option quiet "${FW_QUIET}" A-Z a-z )" | 453 | FW_QUIET="$( config_option quiet "${FW_QUIET}" A-Z a-z )" |
| 342 | FW_VERBOSE="$( config_option verbose "${FW_VERBOSE}" A-Z a-z )" | 454 | FW_VERBOSE="$( config_option verbose "${FW_VERBOSE}" A-Z a-z )" |
| 455 | FW_ORDER_PRE="$( config_option order_prehook "${FW_ORDER_PRE}" A-Z a-z )" | ||
| 456 | FW_ORDER_POST="$( config_option order_posthook "${FW_ORDER_POST}" A-Z a-z )" | ||
| 457 | FW_ORDER_IPTABLES="$( config_option order_iptables "${FW_ORDER_IPTABLES}" A-Z a-z )" | ||
| 458 | FW_ORDER_IPSETS="$( config_option order_ipsets "${FW_ORDER_IPSETS}" A-Z a-z )" | ||
| 343 | 459 | ||
| 344 | 460 | ||
| 345 | # checks and normalizes options | 461 | # checks and normalizes options |
