-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock.c
377 lines (324 loc) · 8.51 KB
/
block.c
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
/*
* Copyright (c) 2010, 2011 Svante J. Kvarnstrom <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <ctype.h>
#include <getopt.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "block.h"
static char *getip(char **, char *);
static char *sjk_strdup(const char *);
static int exists_ip(struct iplist *, char *);
static int is_empty(char *);
static int systemf(const char *, ...);
static struct optlist *optlistinit(void);
static struct optlist *parseargs(int *, char ***);
static void sjk_asprintf(char **, const char *, ...);
static void *sjk_malloc(size_t);
static void add_ip(struct iplist **, char *);
static void ban_ips(struct iplist *, struct optlist *);
static void perrorf(const char *, ...);
static void unban_ips(struct iplist *, struct optlist *);
static void usage(char *);
int
main(int argc, char *argv[])
{
struct optlist *cmdargs;
struct iplist *head = NULL;
char buf[512];
char *p = NULL;
char *q = NULL;
char *ip = NULL;
cmdargs = parseargs(&argc, &argv);
while ((p = fgets(buf, sizeof(buf), stdin)) != NULL) {
if ((q = strchr(p, '\n')) == NULL) {
fprintf(stderr, "Ignoring line that did not fit in buffer.\n");
continue;
}
*q = '\0';
q = p;
while ((q = getip(&ip, q)) != NULL) {
if (is_empty(ip))
continue;
if (exists_ip(head, ip))
continue;
add_ip(&head, ip);
}
}
if (cmdargs->uflag)
unban_ips(head, cmdargs);
else
ban_ips(head, cmdargs);
return 0;
}
/*
* Makes dst point to malloced string containing first IP address found in P.
* Returns NULL if no IP address was found.
*/
static char *
getip(char **dst, char *p)
{
int ip[4];
int off;
if (is_empty(p))
return NULL;
while (!isdigit(*p) && (*p != '\0'))
p++;
if (4 <= sscanf(p, "%d.%d.%d.%d%n", &ip[0], &ip[1], &ip[2], &ip[3], &off)) {
sjk_asprintf(dst, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
p += off;
return p;
} else {
*dst = NULL;
return p+1;
}
}
/*
* Prints help text and exits.
*/
static void
usage(char *p)
{
fprintf(stderr, "Usage: %s [OPTION..]\n\n", p);
fprintf(stderr,
" -h, --help this help text\n"
" -k, --kill-states kill states\n"
" -n, --no-add only kill states\n"
" -t, --table table to add IPs to\n"
" -u, --unblock removes IPs instead of adding them\n"
" -v, --version display program version\n\n"
);
exit(1);
}
/*
* Parses command line arguments and returns a malloced struct optlist
* containing flags and arguments issued by the user.
*/
struct optlist *
parseargs(int *argc, char ***argv)
{
int ch;
struct optlist *cmdargs;
static struct option options[] = {
{ "help", no_argument, NULL, 'h' },
{ "kill-states", no_argument, NULL, 'k' },
{ "no-add", no_argument, NULL, 'n' },
{ "table", required_argument, NULL, 't' },
{ "unblock", no_argument, NULL, 'u' },
{ "version", no_argument, NULL, 'v' },
};
cmdargs = optlistinit();
cmdargs->progname = *argv[0];
while ((ch = getopt_long(*argc, *argv, "hknt:uv", options, NULL)) != -1) {
switch (ch) {
case 'h':
usage(*argv[0]);
break; /* NOT REACHED */
case 'k':
cmdargs->kflag = 1;
break;
case 'n':
cmdargs->nflag = 1;
break;
case 't':
cmdargs->table = sjk_strdup(optarg);
break;
case 'u':
cmdargs->uflag = 1;
break;
case 'v':
printf("%s v%s\n", cmdargs->progname, VERSION);
exit(0);
break; /* NOT REACHED */
default:
usage(*argv[0]);
break; /* NOT REACHED */
}
}
*argc -= optind;
*argv += optind;
if (is_empty(cmdargs->table))
if ((cmdargs->table = getenv("BLOCKTABLE")) == NULL)
cmdargs->table = sjk_strdup(DEFAULTTABLE);
return cmdargs;
}
/*
* Initializes optlist struct used by parseargs()
*/
static struct optlist *
optlistinit(void)
{
struct optlist *cmdargs;
cmdargs = sjk_malloc(sizeof(struct optlist));
cmdargs->kflag = 0;
cmdargs->nflag = 0;
cmdargs->table = NULL;
cmdargs->uflag = 0;
return cmdargs;
}
/*
* strdup() that exits upon error.
*/
static char *
sjk_strdup(const char *src)
{
char *dst;
if ((dst = strdup(src)) == NULL) {
perror("sjk_strdup");
exit(1);
}
return dst;
}
/*
* malloc() that exits upon error.
*/
static void *
sjk_malloc(size_t size)
{
void *ret = NULL;
if ((ret = malloc(size)) == NULL) {
perror("sjk_malloc");
exit(1);
}
return ret;
}
/*
* asprintf() that exits upon error.
*/
static void
sjk_asprintf(char **ret, const char *fmt, ...)
{
va_list va;
va_start(va, fmt);
vasprintf(ret, fmt, va);
if (*ret == NULL) {
perror("sjk_asprintf");
exit(1);
}
va_end(va);
}
/*
* printf-ish perror()
*/
static void
perrorf(const char *fmt, ...)
{
char buf[128];
va_list va;
va_start(va, fmt);
vsnprintf(buf, sizeof(buf), fmt, va);
va_end(va);
perror(buf);
}
/*
* printf-ish system()
*/
static int
systemf(const char *fmt, ...) {
char *string = NULL;
va_list va;
int ret;
va_start(va, fmt);
if (vasprintf(&string, fmt, va) == -1) {
perror("vasprintf");
exit(1);
}
va_end(va);
ret = system(string);
free(string);
return ret;
}
/*
* Checks if string is empty (NULL or \0) and returns true if that is the case.
*/
static int
is_empty(char *s)
{
if ((s == NULL) || (*s == '\0'))
return 1;
else
return 0;
}
/*
* Adds ip to linked list.
*/
static void add_ip(struct iplist **head, char *ip) {
struct iplist *new;
new = sjk_malloc(sizeof(struct iplist));
new->ip = sjk_strdup(ip);
new->next = *head;
*head = new;
}
/*
* Checks if ip is already on the list.
*/
static int exists_ip(struct iplist *head, char *ip) {
int len;
if (head == NULL)
return 0;
len = strlen(ip);
while (head != NULL) {
if ((strncmp(head->ip, ip, len)) == 0)
return 1;
head = head->next;
}
return 0;
}
/*
* Adds ips in linked list to PF table t or kills their states or both,
* depending on command line arguments.
*/
static void
ban_ips(struct iplist *head, struct optlist *c)
{
FILE *PFCTL = NULL;
char *cmd = NULL;
if (!c->nflag) {
sjk_asprintf(&cmd, "/sbin/pfctl -t %s -T add -f -", c->table);
if ((PFCTL = popen(cmd, "w")) == NULL) {
perrorf("Could not popen %s", cmd);
exit(1);
}
free(cmd);
}
while (head != NULL) {
if (!c->nflag)
fprintf(PFCTL, "%s\n", head->ip);
if (c->kflag)
systemf("pfctl -k %s", head->ip);
head = head->next;
}
pclose(PFCTL);
}
static void
unban_ips(struct iplist *head, struct optlist *c)
{
FILE *PFCTL = NULL;
char *cmd = NULL;
sjk_asprintf(&cmd, "/sbin/pfctl -t %s -T del -f -", c->table);
if ((PFCTL = popen(cmd, "w")) == NULL) {
perrorf("Could not popen %s", cmd);
exit(1);
}
free(cmd);
while (head != NULL) {
fprintf(PFCTL, "%s\n", head->ip);
head = head->next;
}
pclose(PFCTL);
}