|
0
|
1 /*
|
|
|
2 * Copyright (c) 2026 Arkadiusz Juszczyk <ark@wobblyhead.net>
|
|
|
3 *
|
|
|
4 * SPDX-License-Identifier: BSD-2-Clause
|
|
|
5 */
|
|
|
6
|
|
|
7 #include <sys/types.h>
|
|
|
8 #include <sys/wait.h>
|
|
|
9
|
|
|
10 #include <errno.h>
|
|
|
11 #include <fcntl.h>
|
|
|
12 #include <signal.h>
|
|
|
13 #include <spawn.h>
|
|
|
14 #include <stdint.h>
|
|
|
15 #include <string.h>
|
|
|
16 #include <syslog.h>
|
|
|
17 #include <unistd.h>
|
|
|
18
|
|
|
19 #include "botblockd.h"
|
|
|
20
|
|
|
21 #define PFCTL_PATH "/sbin/pfctl"
|
|
|
22
|
|
|
23 extern char **environ;
|
|
|
24
|
|
|
25 /*
|
|
|
26 * pfctl - run pfctl with the given arguments, report success
|
|
|
27 *
|
|
|
28 * pfctl is started directly, without a shell, and inherits our
|
|
|
29 * environment, stdin, stdout and stderr, so -T show output reaches the
|
|
|
30 * terminal for the list verb. All signals are reset to their default
|
|
|
31 * action and unblocked in the child, so the SIGPIPE we ignore does not
|
|
|
32 * carry over.
|
|
|
33 *
|
|
|
34 * Parameters:
|
|
|
35 * argv - NULL-terminated argument vector, argv[0] is "pfctl"
|
|
|
36 * discard - true to send pfctl's stdout to /dev/null, stderr is kept
|
|
|
37 *
|
|
|
38 * Returns: 0 if pfctl ran and exited with status 0, -1 otherwise
|
|
|
39 */
|
|
|
40 static int
|
|
|
41 pfctl(const char *argv[], bool discard)
|
|
|
42 {
|
|
|
43 posix_spawn_file_actions_t fa;
|
|
|
44 posix_spawnattr_t attr;
|
|
|
45 sigset_t sigs;
|
|
|
46 pid_t pid;
|
|
|
47 int err, status;
|
|
|
48
|
|
|
49 posix_spawn_file_actions_init(&fa);
|
|
|
50 if (discard)
|
|
|
51 posix_spawn_file_actions_addopen(&fa, STDOUT_FILENO,
|
|
|
52 "/dev/null", O_WRONLY, 0);
|
|
|
53 posix_spawnattr_init(&attr);
|
|
|
54 posix_spawnattr_setflags(&attr,
|
|
|
55 POSIX_SPAWN_SETSIGDEF | POSIX_SPAWN_SETSIGMASK);
|
|
|
56 sigfillset(&sigs);
|
|
|
57 posix_spawnattr_setsigdefault(&attr, &sigs);
|
|
|
58 sigemptyset(&sigs);
|
|
|
59 posix_spawnattr_setsigmask(&attr, &sigs);
|
|
|
60
|
|
|
61 err = posix_spawn(&pid, PFCTL_PATH, &fa, &attr,
|
|
|
62 __DECONST(char **, argv), environ);
|
|
|
63 posix_spawnattr_destroy(&attr);
|
|
|
64 posix_spawn_file_actions_destroy(&fa);
|
|
|
65 if (err != 0) {
|
|
|
66 syslog(LOG_ERR, "cannot run %s: %s", PFCTL_PATH, strerror(err));
|
|
|
67 return (-1);
|
|
|
68 }
|
|
|
69
|
|
|
70 /* a signal arriving while pfctl runs must not abandon the child */
|
|
|
71 while (waitpid(pid, &status, 0) == -1) {
|
|
|
72 if (errno != EINTR) {
|
|
|
73 syslog(LOG_ERR, "waitpid: %s", strerror(errno));
|
|
|
74 return (-1);
|
|
|
75 }
|
|
|
76 }
|
|
|
77 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
|
|
|
78 return (-1);
|
|
|
79 return (0);
|
|
|
80 }
|
|
|
81
|
|
|
82 /*
|
|
|
83 * pf_table_op - run "pfctl -q -t <table> -T <op> [arg]", report success
|
|
|
84 *
|
|
|
85 * -q drops pfctl's "N addresses added/deleted" messages but keeps
|
|
|
86 * errors and the output of -T show.
|
|
|
87 *
|
|
|
88 * Parameters:
|
|
|
89 * op - pfctl table operation (add, delete, show, flush)
|
|
|
90 * arg - address argument, or NULL for whole-table operations
|
|
|
91 * discard - true to send pfctl's stdout to /dev/null
|
|
|
92 *
|
|
|
93 * Returns: 0 on success, -1 on failure
|
|
|
94 */
|
|
|
95 static int
|
|
|
96 pf_table_op(const char *op, const char *arg, bool discard)
|
|
|
97 {
|
|
|
98 /* a NULL arg ends the vector early */
|
|
|
99 const char *argv[] = {
|
|
|
100 "pfctl", "-q", "-t", PF_TABLE, "-T", op, arg, NULL
|
|
|
101 };
|
|
|
102
|
|
|
103 return (pfctl(argv, discard));
|
|
|
104 }
|
|
|
105
|
|
|
106 /*
|
|
|
107 * pf_kill_states - remove all pf states originating from an address
|
|
|
108 *
|
|
|
109 * The block rule only applies to packets without a state, so
|
|
|
110 * connections already established by the address would otherwise
|
|
|
111 * survive the block. -q drops pfctl's "killed N states" message.
|
|
|
112 *
|
|
|
113 * Parameters:
|
|
|
114 * ips - IPv4 address as a string
|
|
|
115 *
|
|
|
116 * Returns: 0 on success, -1 on failure
|
|
|
117 */
|
|
|
118 static int
|
|
|
119 pf_kill_states(const char *ips)
|
|
|
120 {
|
|
|
121 const char *argv[] = { "pfctl", "-q", "-k", ips, NULL };
|
|
|
122
|
|
|
123 return (pfctl(argv, false));
|
|
|
124 }
|
|
|
125
|
|
|
126 /*
|
|
|
127 * pf_block - add an IP to the pf block table and cut its connections
|
|
|
128 *
|
|
|
129 * The address is added to the table before its states are killed, so
|
|
|
130 * it cannot open a new connection in between. A failure to kill the
|
|
|
131 * states is logged but does not fail the block.
|
|
|
132 *
|
|
|
133 * Parameters:
|
|
|
134 * ip - IPv4 address (network byte order)
|
|
|
135 *
|
|
|
136 * Returns: 0 on success, -1 on failure
|
|
|
137 */
|
|
|
138 int
|
|
|
139 pf_block(uint32_t ip)
|
|
|
140 {
|
|
|
141 const char *ips = ip_str(ip);
|
|
|
142
|
|
|
143 if (pf_table_op("add", ips, false) != 0) {
|
|
|
144 syslog(LOG_ERR, "pf block failed for %s", ips);
|
|
|
145 return (-1);
|
|
|
146 }
|
|
|
147 if (pf_kill_states(ips) != 0)
|
|
|
148 syslog(LOG_WARNING, "cannot kill pf states for %s", ips);
|
|
|
149 syslog(LOG_NOTICE, "%s blocked", ips);
|
|
|
150 return (0);
|
|
|
151 }
|
|
|
152
|
|
|
153 /*
|
|
|
154 * pf_unblock - remove an IP from the pf block table
|
|
|
155 *
|
|
|
156 * Parameters:
|
|
|
157 * ip - IPv4 address (network byte order)
|
|
|
158 *
|
|
|
159 * Returns: 0 on success, -1 on failure
|
|
|
160 */
|
|
|
161 int
|
|
|
162 pf_unblock(uint32_t ip)
|
|
|
163 {
|
|
|
164 return (pf_table_op("delete", ip_str(ip), false));
|
|
|
165 }
|
|
|
166
|
|
|
167 /*
|
|
|
168 * pf_show - print the block table, or only check that it is available
|
|
|
169 *
|
|
|
170 * With quiet, the check verb catches a missing table up front rather
|
|
|
171 * than having the first block attempt fail at runtime. stdout is
|
|
|
172 * discarded so the table's contents are not printed, but pfctl's
|
|
|
173 * stderr is kept so its diagnostic (a missing table, pf disabled, or a
|
|
|
174 * permission error) reaches the operator.
|
|
|
175 *
|
|
|
176 * Parameters:
|
|
|
177 * quiet - true to check the table without printing it
|
|
|
178 *
|
|
|
179 * Returns: 0 if the table is present and readable, -1 otherwise
|
|
|
180 */
|
|
|
181 int
|
|
|
182 pf_show(bool quiet)
|
|
|
183 {
|
|
|
184 return (pf_table_op("show", NULL, quiet));
|
|
|
185 }
|
|
|
186
|
|
|
187 /*
|
|
|
188 * pf_flush - remove every entry from the block table
|
|
|
189 *
|
|
|
190 * Used by the "unblock -a" CLI verb, and at daemon startup and
|
|
|
191 * shutdown to leave no blocks without a record behind. Flushing the
|
|
|
192 * whole table is correct in all cases: botblockd owns the table, so
|
|
|
193 * any entry in it is one it placed.
|
|
|
194 *
|
|
|
195 * Returns: 0 on success, -1 on failure
|
|
|
196 */
|
|
|
197 int
|
|
|
198 pf_flush(void)
|
|
|
199 {
|
|
|
200 return (pf_table_op("flush", NULL, false));
|
|
|
201 }
|