comparison src/whitelist.c @ 0:9f8d79c98fd2 0.4.0

initial public release
author Arkadiusz Juszczyk <ark@wobblyhead.net>
date 2026-10-26 11:29 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:9f8d79c98fd2
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/queue.h>
9
10 #include <errno.h>
11 #include <stdint.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <syslog.h>
16
17 #include "botblockd.h"
18
19 struct wl {
20 SLIST_ENTRY(wl) next;
21 uint32_t net; /* network address, network order */
22 uint32_t mask; /* netmask, network order */
23 };
24
25 SLIST_HEAD(wl_head, wl);
26 static struct wl_head wl = SLIST_HEAD_INITIALIZER(wl);
27
28 /*
29 * free_list - free all entries of a whitelist
30 *
31 * Parameters:
32 * head - list to empty
33 */
34 static void
35 free_list(struct wl_head *head)
36 {
37 struct wl *e;
38
39 while ((e = SLIST_FIRST(head)) != NULL) {
40 SLIST_REMOVE_HEAD(head, next);
41 free(e);
42 }
43 }
44
45 /*
46 * load_whitelist - read a whitelist file and replace the current list
47 *
48 * The file is read into a new list; the current list is replaced only
49 * if the whole file is read without error, otherwise it stays in use.
50 * Each entry is an IPv4 address or CIDR subnet (see parse_cidr). Blank
51 * lines and lines whose first non-blank character is '#' are skipped.
52 * An entry that fails to parse fails the load, mirroring the strict
53 * config parser: an operator typo must not be silently dropped. A
54 * missing file is not an error and gives an empty whitelist.
55 *
56 * Parameters:
57 * path - whitelist file
58 *
59 * Returns: 0 on success, -1 on an unreadable file or invalid entry
60 */
61 int
62 load_whitelist(const char *path)
63 {
64 struct wl_head nwl = SLIST_HEAD_INITIALIZER(nwl);
65 struct wl *e;
66 FILE *fp;
67 size_t cap = 0;
68 char *entry, *line = NULL;
69 uint32_t mask, net;
70 int count = 0, lineno = 0, rc = 0;
71
72 fp = fopen(path, "r");
73 if (fp == NULL) {
74 if (errno != ENOENT) {
75 syslog(LOG_ERR, "cannot open whitelist %s: %s",
76 path, strerror(errno));
77 return (-1);
78 }
79 syslog(LOG_NOTICE, "no whitelist at %s", path);
80 free_list(&wl);
81 return (0);
82 }
83
84 while ((entry = read_entry(fp, &line, &cap, &lineno)) != NULL) {
85 if (parse_cidr(entry, &net, &mask) != 0) {
86 syslog(LOG_ERR, "whitelist: invalid entry '%s' on line %d",
87 entry, lineno);
88 rc = -1;
89 break;
90 }
91 e = malloc(sizeof(*e));
92 if (e == NULL) {
93 syslog(LOG_ERR, "whitelist: out of memory");
94 rc = -1;
95 break;
96 }
97 e->net = net;
98 e->mask = mask;
99 SLIST_INSERT_HEAD(&nwl, e, next);
100 count++;
101 }
102 if (rc == 0 && ferror(fp)) {
103 syslog(LOG_ERR, "cannot read whitelist %s", path);
104 rc = -1;
105 }
106 free(line);
107 fclose(fp);
108 if (rc != 0) {
109 free_list(&nwl);
110 return (rc);
111 }
112
113 free_list(&wl);
114 SLIST_SWAP(&wl, &nwl, wl);
115 syslog(LOG_INFO, "loaded %d whitelist entries", count);
116 return (0);
117 }
118
119 /*
120 * is_whitelisted - report whether an IP is exempt from blocking
121 *
122 * Matches the entries of the whitelist file (host or CIDR subnet).
123 *
124 * Parameters:
125 * ip - IPv4 address (network byte order)
126 *
127 * Returns: true if whitelisted, false otherwise
128 */
129 bool
130 is_whitelisted(uint32_t ip)
131 {
132 struct wl *e;
133
134 SLIST_FOREACH(e, &wl, next) {
135 if ((ip & e->mask) == e->net)
136 return (true);
137 }
138 return (false);
139 }