view src/whitelist.c @ 1:30f396bc751c default tip

Added tag 0.4.0 for changeset 9f8d79c98fd2
author Arkadiusz Juszczyk <ark@wobblyhead.net>
date 2026-10-26 11:29 +0100
parents 9f8d79c98fd2
children
line wrap: on
line source

/*
 * Copyright (c) 2026 Arkadiusz Juszczyk <ark@wobblyhead.net>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <sys/types.h>
#include <sys/queue.h>

#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>

#include "botblockd.h"

struct wl {
    SLIST_ENTRY(wl) next;
    uint32_t        net;           /* network address, network order */
    uint32_t        mask;          /* netmask, network order */
};

SLIST_HEAD(wl_head, wl);
static struct wl_head wl = SLIST_HEAD_INITIALIZER(wl);

/*
 * free_list - free all entries of a whitelist
 *
 * Parameters:
 *   head - list to empty
 */
static void
free_list(struct wl_head *head)
{
    struct wl *e;

    while ((e = SLIST_FIRST(head)) != NULL) {
        SLIST_REMOVE_HEAD(head, next);
        free(e);
    }
}

/*
 * load_whitelist - read a whitelist file and replace the current list
 *
 * The file is read into a new list; the current list is replaced only
 * if the whole file is read without error, otherwise it stays in use.
 * Each entry is an IPv4 address or CIDR subnet (see parse_cidr).  Blank
 * lines and lines whose first non-blank character is '#' are skipped.
 * An entry that fails to parse fails the load, mirroring the strict
 * config parser: an operator typo must not be silently dropped.  A
 * missing file is not an error and gives an empty whitelist.
 *
 * Parameters:
 *   path - whitelist file
 *
 * Returns: 0 on success, -1 on an unreadable file or invalid entry
 */
int
load_whitelist(const char *path)
{
    struct wl_head nwl = SLIST_HEAD_INITIALIZER(nwl);
    struct wl *e;
    FILE *fp;
    size_t cap = 0;
    char *entry, *line = NULL;
    uint32_t mask, net;
    int count = 0, lineno = 0, rc = 0;

    fp = fopen(path, "r");
    if (fp == NULL) {
        if (errno != ENOENT) {
            syslog(LOG_ERR, "cannot open whitelist %s: %s",
                path, strerror(errno));
            return (-1);
        }
        syslog(LOG_NOTICE, "no whitelist at %s", path);
        free_list(&wl);
        return (0);
    }

    while ((entry = read_entry(fp, &line, &cap, &lineno)) != NULL) {
        if (parse_cidr(entry, &net, &mask) != 0) {
            syslog(LOG_ERR, "whitelist: invalid entry '%s' on line %d",
                entry, lineno);
            rc = -1;
            break;
        }
        e = malloc(sizeof(*e));
        if (e == NULL) {
            syslog(LOG_ERR, "whitelist: out of memory");
            rc = -1;
            break;
        }
        e->net = net;
        e->mask = mask;
        SLIST_INSERT_HEAD(&nwl, e, next);
        count++;
    }
    if (rc == 0 && ferror(fp)) {
        syslog(LOG_ERR, "cannot read whitelist %s", path);
        rc = -1;
    }
    free(line);
    fclose(fp);
    if (rc != 0) {
        free_list(&nwl);
        return (rc);
    }

    free_list(&wl);
    SLIST_SWAP(&wl, &nwl, wl);
    syslog(LOG_INFO, "loaded %d whitelist entries", count);
    return (0);
}

/*
 * is_whitelisted - report whether an IP is exempt from blocking
 *
 * Matches the entries of the whitelist file (host or CIDR subnet).
 *
 * Parameters:
 *   ip - IPv4 address (network byte order)
 *
 * Returns: true if whitelisted, false otherwise
 */
bool
is_whitelisted(uint32_t ip)
{
    struct wl *e;

    SLIST_FOREACH(e, &wl, next) {
        if ((ip & e->mask) == e->net)
            return (true);
    }
    return (false);
}