2022-03-16 20:30:31 +00:00
|
|
|
#!/usr/bin/env sh
|
2022-03-16 20:46:15 +00:00
|
|
|
# shellcheck disable=SC3043 #https://github.com/koalaman/shellcheck/wiki/SC3043#exceptions
|
|
|
|
|
2022-01-30 23:05:28 +00:00
|
|
|
# Pi-hole: A black hole for Internet advertisements
|
|
|
|
# (c) 2017 Pi-hole, LLC (https://pi-hole.net)
|
|
|
|
# Network-wide ad blocking via your own hardware.
|
|
|
|
#
|
|
|
|
# Script to hold utility functions for use in other scripts
|
|
|
|
#
|
|
|
|
# This file is copyright under the latest version of the EUPL.
|
|
|
|
# Please see LICENSE file for your rights under this license.
|
|
|
|
|
|
|
|
# Basic Housekeeping rules
|
|
|
|
# - Functions must be self contained
|
2022-04-21 18:56:21 +00:00
|
|
|
# - Functions should be grouped with other similar functions
|
2022-01-30 23:05:28 +00:00
|
|
|
# - Functions must be documented
|
|
|
|
# - New functions must have a test added for them in test/test_any_utils.py
|
|
|
|
|
|
|
|
#######################
|
2022-04-14 21:53:38 +00:00
|
|
|
# Takes Three arguments: file, key, and value.
|
2022-03-16 17:42:01 +00:00
|
|
|
#
|
2022-01-30 23:05:28 +00:00
|
|
|
# Checks the target file for the existence of the key
|
|
|
|
# - If it exists, it changes the value
|
|
|
|
# - If it does not exist, it adds the value
|
|
|
|
#
|
|
|
|
# Example usage:
|
2022-04-14 21:53:38 +00:00
|
|
|
# addOrEditKeyValPair "/etc/pihole/setupVars.conf" "BLOCKING_ENABLED" "true"
|
2022-01-30 23:05:28 +00:00
|
|
|
#######################
|
|
|
|
addOrEditKeyValPair() {
|
2022-03-16 20:46:15 +00:00
|
|
|
local file="${1}"
|
|
|
|
local key="${2}"
|
|
|
|
local value="${3}"
|
2022-03-16 17:42:01 +00:00
|
|
|
|
2022-11-09 20:25:09 +00:00
|
|
|
# touch file to prevent grep error if file does not exist yet
|
|
|
|
touch "${file}"
|
|
|
|
|
2022-04-14 21:53:38 +00:00
|
|
|
if grep -q "^${key}=" "${file}"; then
|
2022-09-21 07:24:44 +00:00
|
|
|
# Key already exists in file, modify the value
|
|
|
|
sed -i "/^${key}=/c\\${key}=${value}" "${file}"
|
2022-01-30 23:05:28 +00:00
|
|
|
else
|
2022-04-14 21:53:38 +00:00
|
|
|
# Key does not already exist, add it and it's value
|
|
|
|
echo "${key}=${value}" >> "${file}"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
#######################
|
|
|
|
# Takes two arguments: file, and key.
|
|
|
|
# Adds a key to target file
|
|
|
|
#
|
|
|
|
# Example usage:
|
|
|
|
# addKey "/etc/dnsmasq.d/01-pihole.conf" "log-queries"
|
|
|
|
#######################
|
|
|
|
addKey(){
|
|
|
|
local file="${1}"
|
|
|
|
local key="${2}"
|
|
|
|
|
2022-11-09 20:25:09 +00:00
|
|
|
# touch file to prevent grep error if file does not exist yet
|
|
|
|
touch "${file}"
|
|
|
|
|
2023-03-17 18:47:26 +00:00
|
|
|
# Match key against entire line, using both anchors. We assume
|
|
|
|
# that the file's keys never have bounding whitespace. Anchors
|
|
|
|
# are necessary to ensure the key is considered absent when it
|
|
|
|
# is a substring of another key present in the file.
|
2023-03-17 02:36:22 +00:00
|
|
|
if ! grep -q "^${key}$" "${file}"; then
|
2022-09-21 07:24:44 +00:00
|
|
|
# Key does not exist, add it.
|
|
|
|
echo "${key}" >> "${file}"
|
2022-01-30 23:05:28 +00:00
|
|
|
fi
|
|
|
|
}
|
2022-02-20 21:24:17 +00:00
|
|
|
|
2022-03-16 17:42:01 +00:00
|
|
|
#######################
|
2022-04-14 21:53:38 +00:00
|
|
|
# Takes two arguments: file, and key.
|
|
|
|
# Deletes a key or key/value pair from target file
|
2022-03-16 17:42:01 +00:00
|
|
|
#
|
|
|
|
# Example usage:
|
2022-04-04 21:02:26 +00:00
|
|
|
# removeKey "/etc/pihole/setupVars.conf" "PIHOLE_DNS_1"
|
2022-03-16 17:42:01 +00:00
|
|
|
#######################
|
|
|
|
removeKey() {
|
2022-04-04 21:02:26 +00:00
|
|
|
local file="${1}"
|
|
|
|
local key="${2}"
|
2022-03-16 17:42:01 +00:00
|
|
|
sed -i "/^${key}/d" "${file}"
|
|
|
|
}
|
|
|
|
|
2022-07-26 12:38:03 +00:00
|
|
|
|
|
|
|
#######################
|
2022-09-21 07:24:44 +00:00
|
|
|
# returns FTL's current telnet API port based on the setting in /etc/pihole-FTL.conf
|
2022-09-25 16:16:20 +00:00
|
|
|
########################
|
2022-02-20 21:24:17 +00:00
|
|
|
getFTLAPIPort(){
|
2022-09-21 07:24:44 +00:00
|
|
|
local FTLCONFFILE="/etc/pihole/pihole-FTL.conf"
|
2022-07-26 12:38:03 +00:00
|
|
|
local DEFAULT_FTL_PORT=4711
|
|
|
|
local ftl_api_port
|
|
|
|
|
2022-09-21 07:24:44 +00:00
|
|
|
if [ -s "$FTLCONFFILE" ]; then
|
|
|
|
# if FTLPORT is not set in pihole-FTL.conf, use the default port
|
|
|
|
ftl_api_port="$({ grep '^FTLPORT=' "${FTLCONFFILE}" || echo "${DEFAULT_FTL_PORT}"; } | cut -d'=' -f2-)"
|
2022-09-26 21:40:09 +00:00
|
|
|
# Exploit prevention: set the port to the default port if there is malicious (non-numeric)
|
|
|
|
# content set in pihole-FTL.conf
|
|
|
|
expr "${ftl_api_port}" : "[^[:digit:]]" > /dev/null && ftl_api_port="${DEFAULT_FTL_PORT}"
|
2022-09-21 07:24:44 +00:00
|
|
|
else
|
|
|
|
# if there is no pihole-FTL.conf, use the default port
|
|
|
|
ftl_api_port="${DEFAULT_FTL_PORT}"
|
2022-07-26 12:38:03 +00:00
|
|
|
fi
|
|
|
|
|
2022-09-21 07:24:44 +00:00
|
|
|
echo "${ftl_api_port}"
|
2022-07-26 12:38:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#######################
|
|
|
|
# returns path of FTL's PID file
|
|
|
|
#######################
|
|
|
|
getFTLPIDFile() {
|
2022-03-16 20:30:31 +00:00
|
|
|
local FTLCONFFILE="/etc/pihole/pihole-FTL.conf"
|
2022-07-26 12:38:03 +00:00
|
|
|
local DEFAULT_PID_FILE="/run/pihole-FTL.pid"
|
|
|
|
local FTL_PID_FILE
|
2022-02-20 21:24:17 +00:00
|
|
|
|
2022-07-26 12:38:03 +00:00
|
|
|
if [ -s "${FTLCONFFILE}" ]; then
|
|
|
|
# if PIDFILE is not set in pihole-FTL.conf, use the default path
|
|
|
|
FTL_PID_FILE="$({ grep '^PIDFILE=' "${FTLCONFFILE}" || echo "${DEFAULT_PID_FILE}"; } | cut -d'=' -f2-)"
|
|
|
|
else
|
|
|
|
# if there is no pihole-FTL.conf, use the default path
|
|
|
|
FTL_PID_FILE="${DEFAULT_PID_FILE}"
|
2022-02-20 21:24:17 +00:00
|
|
|
fi
|
|
|
|
|
2022-07-26 12:38:03 +00:00
|
|
|
echo "${FTL_PID_FILE}"
|
|
|
|
}
|
|
|
|
|
|
|
|
#######################
|
|
|
|
# returns FTL's PID based on the content of the pihole-FTL.pid file
|
|
|
|
#
|
|
|
|
# Takes one argument: path to pihole-FTL.pid
|
|
|
|
# Example getFTLPID "/run/pihole-FTL.pid"
|
|
|
|
#######################
|
|
|
|
getFTLPID() {
|
|
|
|
local FTL_PID_FILE="${1}"
|
|
|
|
local FTL_PID
|
|
|
|
|
|
|
|
if [ -s "${FTL_PID_FILE}" ]; then
|
|
|
|
# -s: FILE exists and has a size greater than zero
|
|
|
|
FTL_PID="$(cat "${FTL_PID_FILE}")"
|
|
|
|
# Exploit prevention: unset the variable if there is malicious content
|
|
|
|
# Verify that the value read from the file is numeric
|
|
|
|
expr "${FTL_PID}" : "[^[:digit:]]" > /dev/null && unset FTL_PID
|
|
|
|
fi
|
|
|
|
|
|
|
|
# If FTL is not running, or the PID file contains malicious stuff, substitute
|
|
|
|
# negative PID to signal this
|
|
|
|
FTL_PID=${FTL_PID:=-1}
|
|
|
|
echo "${FTL_PID}"
|
2022-02-20 21:24:17 +00:00
|
|
|
}
|