This file is indexed.

/lib/ufw/ufw-init-functions is in ufw 0.35-5.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
#!/bin/sh
#
# ufw-init-functions: functions used by ufw-init and distribution initscripts
#
# Copyright 2008-2015 Canonical Ltd.
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License version 3,
#    as published by the Free Software Foundation.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
set -e

PATH="/sbin:/bin:/usr/sbin:/usr/bin"

for s in "${DATA_DIR}/etc/default/ufw" "${DATA_DIR}/etc/ufw/ufw.conf" ; do
    if [ -s "$s" ]; then
        . "$s"
    else
        echo "Could not find $s (aborting)"
        exit 1
    fi
done

RULES_PATH="${DATA_DIR}/etc/ufw"
USER_PATH="${DATA_DIR}/etc/ufw"

flush_builtins() {
    error=""
    execs="iptables"
    if ip6tables -L INPUT -n >/dev/null 2>&1; then
        execs="$execs ip6tables"
    fi

    for exe in $execs
    do
        $exe -F || error="yes"
        $exe -X || error="yes"
        $exe -P INPUT ACCEPT || error="yes"
        $exe -P OUTPUT ACCEPT || error="yes"
        $exe -P FORWARD ACCEPT || error="yes"

        # now handle the mangle table
        if $exe -t mangle -L -n >/dev/null 2>&1; then
            for i in INPUT OUTPUT FORWARD PREROUTING POSTROUTING ; do
                $exe -t mangle -F $i || error="yes"
                $exe -t mangle -P $i ACCEPT || error="yes"
            done
        fi
    done

    # now handle the nat table
    if iptables -t nat -L -n >/dev/null 2>&1; then
        for i in OUTPUT PREROUTING POSTROUTING ; do
            iptables -t nat -F $i || error="yes"
            iptables -t nat -P $i ACCEPT || error="yes"
        done
    fi

    if [ "$error" = "yes" ]; then
        return 1
    fi
}

chains_command() {
    flag="$1"
    type=""
    exe="iptables"
    if [ "$2" = "6" ]; then
        type="$2"
        exe="ip6tables"
    fi

    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
        if [ "$UFW_INIT_DEBUG" = "yes" ]; then
            echo "$exe $flag $c" >&2
            $exe $flag $c || true
        else
            $exe $flag $c 2>/dev/null || true
        fi
    done
}

delete_chains() {
    chains_command -F $1
    chains_command -Z $1

    # Delete the secondary chains to reduce clutter, but keep the primary ones
    # so that the primary chains don't leave the built-in chains just to come
    # back later in a different place. This means that some (empty) chains will
    # linger until the next boot after disabling ufw.
    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
        if [ "$UFW_INIT_DEBUG" = "yes" ]; then
            echo "$exe $flag $c" >&2
            $exe -X $c || true
        else
            $exe -X $c 2>/dev/null || true
        fi
    done
}

ufw_start() {
    out=""
    if [ "$ENABLED" = "yes" ] || [ "$ENABLED" = "YES" ]; then
        if iptables -L ufw-user-input -n >/dev/null 2>&1 ; then
            echo "Firewall already started, use 'force-reload'"
            return 0
        fi
        for m in $IPT_MODULES
        do
            modprobe $m || true
        done

        if [ "$MANAGE_BUILTINS" = "yes" ]; then
            flush_builtins
        fi

        if [ -x "$RULES_PATH/before.init" ]; then
            if ! "$RULES_PATH/before.init" start ; then
                error="yes"
                out="${out}\n'$RULES_PATH/before.init start' exited with error"
            fi
        fi

        execs="iptables"

        # IPv6 setup
        if [ "$IPV6" = "yes" ] || [ "$IPV6" = "YES" ]; then
            if ip6tables -L INPUT -n >/dev/null 2>&1; then
                execs="$execs ip6tables"
            else
                out="${out}\nProblem loading ipv6 (skipping)"
            fi
        else
            if ip6tables -L INPUT -n >/dev/null 2>&1; then
                # IPv6 support disabled but available in the kernel, so
                # default DROP and accept all on loopback
                delete_chains 6 || error="yes"

                printf "*filter\n"\
":INPUT DROP [0:0]\n"\
":FORWARD DROP [0:0]\n"\
":OUTPUT DROP [0:0]\n"\
"-A INPUT -i lo -j ACCEPT\n"\
"-A OUTPUT -o lo -j ACCEPT\n"\
"COMMIT\n" | ip6tables-restore || error="yes"

                if [ "$error" = "yes" ]; then
                    out="${out}\nProblem loading ipv6 (skipping)"
                fi
            fi
        fi

        for exe in $execs
        do
            type=""
            if [ "$exe" = "ip6tables" ]; then
                type="6"
            fi
            BEFORE_RULES="$RULES_PATH/before${type}.rules"
            AFTER_RULES="$RULES_PATH/after${type}.rules"
            USER_RULES="$USER_PATH/user${type}.rules"

            # set the default policy
            input_pol="$DEFAULT_INPUT_POLICY"
            if [ "$DEFAULT_INPUT_POLICY" = "REJECT" ]; then
                input_pol="DROP"
            fi

            output_pol="$DEFAULT_OUTPUT_POLICY"
            if [ "$DEFAULT_OUTPUT_POLICY" = "REJECT" ]; then
                output_pol="DROP"
            fi

            forward_pol="$DEFAULT_FORWARD_POLICY"
            if [ "$DEFAULT_FORWARD_POLICY" = "REJECT" ]; then
                forward_pol="DROP"
            fi

            printf "*filter\n"\
"# builtin chains\n"\
":INPUT %s [0:0]\n"\
":FORWARD %s [0:0]\n"\
":OUTPUT %s [0:0]\n"\
"COMMIT\n" $input_pol $forward_pol $output_pol | $exe-restore -n || error="yes"

            # flush the chains (if they exist)
            if $exe -L ufw${type}-before-logging-input -n >/dev/null 2>&1 ; then
                delete_chains $type || error="yes"
            else
                # setup all the primary chains
                printf "*filter\n"\
"# primary chains\n"\
":ufw${type}-before-logging-input - [0:0]\n"\
":ufw${type}-before-logging-output - [0:0]\n"\
":ufw${type}-before-logging-forward - [0:0]\n"\
":ufw${type}-before-input - [0:0]\n"\
":ufw${type}-before-output - [0:0]\n"\
":ufw${type}-before-forward - [0:0]\n"\
":ufw${type}-after-input - [0:0]\n"\
":ufw${type}-after-output - [0:0]\n"\
":ufw${type}-after-forward - [0:0]\n"\
":ufw${type}-after-logging-input - [0:0]\n"\
":ufw${type}-after-logging-output - [0:0]\n"\
":ufw${type}-after-logging-forward - [0:0]\n"\
":ufw${type}-reject-input - [0:0]\n"\
":ufw${type}-reject-output - [0:0]\n"\
":ufw${type}-reject-forward - [0:0]\n"\
":ufw${type}-track-input - [0:0]\n"\
":ufw${type}-track-output - [0:0]\n"\
":ufw${type}-track-forward - [0:0]\n"\
"\n"\
"-A INPUT -j ufw${type}-before-logging-input\n"\
"-A INPUT -j ufw${type}-before-input\n"\
"-A INPUT -j ufw${type}-after-input\n"\
"-A INPUT -j ufw${type}-after-logging-input\n"\
"-A INPUT -j ufw${type}-reject-input\n"\
"-A INPUT -j ufw${type}-track-input\n"\
"\n"\
"-A OUTPUT -j ufw${type}-before-logging-output\n"\
"-A OUTPUT -j ufw${type}-before-output\n"\
"-A OUTPUT -j ufw${type}-after-output\n"\
"-A OUTPUT -j ufw${type}-after-logging-output\n"\
"-A OUTPUT -j ufw${type}-reject-output\n"\
"-A OUTPUT -j ufw${type}-track-output\n"\
"\n"\
"-A FORWARD -j ufw${type}-before-logging-forward\n"\
"-A FORWARD -j ufw${type}-before-forward\n"\
"-A FORWARD -j ufw${type}-after-forward\n"\
"-A FORWARD -j ufw${type}-after-logging-forward\n"\
"-A FORWARD -j ufw${type}-reject-forward\n"\
"-A FORWARD -j ufw${type}-track-forward\n"\
"COMMIT\n" | $exe-restore -n || error="yes"
            fi

            # add reject policy
            if [ "$DEFAULT_INPUT_POLICY" = "REJECT" ]; then
                printf "*filter\n"\
"-A ufw${type}-reject-input -j REJECT\n"\
"COMMIT\n" | $exe-restore -n || error="yes"
            fi
            if [ "$DEFAULT_OUTPUT_POLICY" = "REJECT" ]; then
                printf "*filter\n"\
"-A ufw${type}-reject-output -j REJECT\n"\
"COMMIT\n" | $exe-restore -n || error="yes"
            fi
            if [ "$DEFAULT_FORWARD_POLICY" = "REJECT" ]; then
                printf "*filter\n"\
"-A ufw${type}-reject-forward -j REJECT\n"\
"COMMIT\n" | $exe-restore -n || error="yes"
            fi

            # add tracking policy
            if [ "$DEFAULT_INPUT_POLICY" = "ACCEPT" ]; then
                printf "*filter\n"\
"-A ufw${type}-track-input -p tcp -m conntrack --ctstate NEW -j ACCEPT\n"\
"-A ufw${type}-track-input -p udp -m conntrack --ctstate NEW -j ACCEPT\n"\
"COMMIT\n" | $exe-restore -n || error="yes"
            fi

            if [ "$DEFAULT_OUTPUT_POLICY" = "ACCEPT" ]; then
                printf "*filter\n"\
"-A ufw${type}-track-output -p tcp -m conntrack --ctstate NEW -j ACCEPT\n"\
"-A ufw${type}-track-output -p udp -m conntrack --ctstate NEW -j ACCEPT\n"\
"COMMIT\n" | $exe-restore -n || error="yes"
            fi

            if [ "$DEFAULT_FORWARD_POLICY" = "ACCEPT" ]; then
                printf "*filter\n"\
"-A ufw${type}-track-forward -p tcp -m conntrack --ctstate NEW -j ACCEPT\n"\
"-A ufw${type}-track-forward -p udp -m conntrack --ctstate NEW -j ACCEPT\n"\
"COMMIT\n" | $exe-restore -n || error="yes"
            fi

            # now setup the secondary 'logging-deny' chains
            if ! $exe -L ufw${type}-logging-deny -n >/dev/null 2>&1 ; then
                printf "*filter\n"\
":ufw${type}-logging-deny - [0:0]\n"\
":ufw${type}-logging-allow - [0:0]\n"\
"COMMIT\n" | $exe-restore -n || error="yes"
            fi

            # now setup the secondary 'skip to policy' chains
            if ! $exe -L ufw${type}-skip-to-policy-input -n >/dev/null 2>&1 ; then
                printf "*filter\n"\
":ufw${type}-skip-to-policy-input - [0:0]\n"\
":ufw${type}-skip-to-policy-output - [0:0]\n"\
":ufw${type}-skip-to-policy-forward - [0:0]\n"\
"-A ufw${type}-skip-to-policy-input -j %s\n"\
"-A ufw${type}-skip-to-policy-output -j %s\n"\
"-A ufw${type}-skip-to-policy-forward -j %s\n"\
"COMMIT\n" $DEFAULT_INPUT_POLICY $DEFAULT_OUTPUT_POLICY $DEFAULT_FORWARD_POLICY | $exe-restore -n || error="yes"
            fi

            # now ip[6]tables-restore before*.rules. This resets the following
            # chains:
            #   ufw-before-input
            #   ufw-before-output
            #   ufw-before-forward
            #
            # and sets the following:
            #   ufw-not-local
            if [ -s "$BEFORE_RULES" ]; then
                if ! $exe-restore -n < "$BEFORE_RULES" ; then
                    out="${out}\nProblem running '$BEFORE_RULES'"
                    error="yes"
                fi
            else
                out="${out}\nCouldn't find '$BEFORE_RULES'"
                error="yes"
            fi

            # now ip[6]tables-restore after*.rules. This resets the following
            # chains:
            #   ufw-after-input
            #   ufw-after-output
            #   ufw-after-forward
            if [ -s "$AFTER_RULES" ]; then
                if ! $exe-restore -n < "$AFTER_RULES" ; then
                    out="${out}\nProblem running '$AFTER_RULES'"
                    error="yes"
                fi
            else
                out="${out}\nCouldn't find '$AFTER_RULES'"
                error="yes"
            fi

            # user chains
            if [ -s "$USER_RULES" ]; then
                # setup the secondary 'user' chains
                if ! $exe -L ufw${type}-user-input -n >/dev/null 2>&1 ; then
                    printf "*filter\n"\
":ufw${type}-user-input - [0:0]\n"\
":ufw${type}-user-output - [0:0]\n"\
":ufw${type}-user-forward - [0:0]\n"\
":ufw${type}-user-logging-input - [0:0]\n"\
":ufw${type}-user-logging-output - [0:0]\n"\
":ufw${type}-user-logging-forward - [0:0]\n"\
":ufw${type}-user-limit - [0:0]\n"\
":ufw${type}-user-limit-accept - [0:0]\n"\
"COMMIT\n" | $exe-restore -n || error="yes"
                fi

                # now ip[6]tables-restore user*.rules. This resets the following
                # chains:
                #   ufw-before-logging-input
                #   ufw-before-logging-output
                #   ufw-before-logging-forward
                #   ufw-after-logging-input
                #   ufw-after-logging-output
                #   ufw-after-logging-forward
                #   ufw-logging-deny
                #   ufw-logging-allow
                #   ufw-after-input
                #   ufw-after-output
                #   ufw-after-forward
                #   ufw-user-limit
                #   ufw-user-limit-accept
                if ! $exe-restore -n < "$USER_RULES" ; then
                    out="${out}\nProblem running '$USER_RULES'"
                    error="yes"
                fi

                # now hooks these into the primary chains
                printf "*filter\n"\
"-A ufw${type}-before-input -j ufw${type}-user-input\n"\
"-A ufw${type}-before-output -j ufw${type}-user-output\n"\
"-A ufw${type}-before-forward -j ufw${type}-user-forward\n"\
"COMMIT\n" | $exe-restore -n || error="yes"
            else
                out="${out}\nCouldn't find '$USER_RULES'"
                error="yes"
            fi
        done

        if [ ! -z "$IPT_SYSCTL" ] && [ -s "$IPT_SYSCTL" ]; then
            sysctl -e -q -p $IPT_SYSCTL || true
        fi

        if [ -x "$RULES_PATH/after.init" ]; then
            if ! "$RULES_PATH/after.init" start ; then
                error="yes"
                out="${out}\n'$RULES_PATH/after.init start' exited with error"
            fi
        fi

        if [ "$error" = "yes" ]; then
            /bin/echo -e "$out"
            return 1
        fi
    else
        out="Skip starting firewall: ufw (not enabled)"
    fi
    if [ ! -z "$out" ]; then
        /bin/echo -e "$out"
    fi
}

ufw_stop() {
    if [ "$1" != "--force" ] && [ "$ENABLED" != "yes" ] && [ "$ENABLED" != "YES" ]; then
        echo "Skip stopping firewall: ufw (not enabled)"
        return 0
    fi

    error=""

    if [ -x "$RULES_PATH/before.init" ]; then
        if ! "$RULES_PATH/before.init" stop ; then
            error="yes"
            out="${out}\n'$RULES_PATH/before.init stop' exited with error"
        fi
    fi

    # If we manage the builtins, just return
    if [ "$MANAGE_BUILTINS" = "yes" ]; then
        flush_builtins || error="yes"
        if [ -x "$RULES_PATH/after.init" ]; then
            "$RULES_PATH/after.init" stop || error="yes"
        fi
        if [ "$error" = "yes" ]; then
            return 1
        fi
        return 0
    fi

    execs="iptables"
    if ip6tables -L INPUT -n >/dev/null 2>&1; then
        execs="$execs ip6tables"
    fi

    for exe in $execs
    do
        type=""
        if [ "$exe" = "ip6tables" ]; then
            type="6"
        fi
        delete_chains $type || error="yes"
        $exe -P INPUT ACCEPT || error="yes"
        $exe -P OUTPUT ACCEPT || error="yes"
        $exe -P FORWARD ACCEPT || error="yes"
    done

    if [ -x "$RULES_PATH/after.init" ]; then
        if ! "$RULES_PATH/after.init" stop ; then
            error="yes"
        fi
    fi

    if [ "$error" = "yes" ]; then
        return 1
    fi
    return 0
}

ufw_reload() {
    if [ "$ENABLED" = "yes" ] || [ "$ENABLED" = "YES" ]; then
        if [ -x "$RULES_PATH/before.init" ]; then
            "$RULES_PATH/before.init" stop || return 1
        fi
        ufw_stop || return "$?"
        if [ -x "$RULES_PATH/after.init" ]; then
            "$RULES_PATH/after.init" stop || return 1
        fi

        if [ -x "$RULES_PATH/before.init" ]; then
            "$RULES_PATH/before.init" start || return 1
        fi
        ufw_start || return "$?"
        if [ -x "$RULES_PATH/after.init" ]; then
            "$RULES_PATH/after.init" start || return 1
        fi
    else
        echo "Skipping $1 (not enabled)"
    fi
    return 0
}

ufw_status() {
    err=""
    iptables -L ufw-user-input -n >/dev/null 2>&1 || {
        echo "Firewall is not running"
        return 3
    }

    if [ "$IPV6" = "yes" ] || [ "$IPV6" = "YES" ]; then
        ip6tables -L ufw6-user-input -n >/dev/null 2>&1 || {
            # unknown state: ipv4 ok, but ipv6 isn't
            echo "Firewall in inconsistent state (IPv6 enabled but not running)"
            return 4
        }
    fi

    echo "Firewall is running"
    return 0
}