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
|
|
|
|
# - Functions must be added in alphabetical order
|
|
|
|
# - Functions must be documented
|
|
|
|
# - New functions must have a test added for them in test/test_any_utils.py
|
|
|
|
|
|
|
|
#######################
|
2022-03-16 17:42:01 +00:00
|
|
|
# Takes either
|
2022-03-16 20:46:15 +00:00
|
|
|
# - Three arguments: file, key, and value.
|
|
|
|
# - Two arguments: file, and key.
|
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-03-16 20:46:15 +00:00
|
|
|
# addOrEditKeyValuePair "/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-03-16 20:30:31 +00:00
|
|
|
if [ "${value}" != "" ]; then
|
2022-04-04 21:14:10 +00:00
|
|
|
# value has a value, so it is a key-value pair
|
2022-03-16 17:42:01 +00:00
|
|
|
if grep -q "^${key}=" "${file}"; then
|
|
|
|
# Key already exists in file, modify the value
|
|
|
|
sed -i "/^${key}=/c\\${key}=${value}" "${file}"
|
|
|
|
else
|
|
|
|
# Key does not already exist, add it and it's value
|
|
|
|
echo "${key}=${value}" >> "${file}"
|
|
|
|
fi
|
2022-01-30 23:05:28 +00:00
|
|
|
else
|
2022-03-16 17:42:01 +00:00
|
|
|
# value has no value, so it is just a key. Add it if it does not already exist
|
|
|
|
if ! grep -q "^${key}" "${file}"; then
|
|
|
|
# Key does not exist, add it.
|
|
|
|
echo "${key}" >> "${file}"
|
|
|
|
fi
|
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-04 21:14:10 +00:00
|
|
|
# Takes two arguments file, and key.
|
2022-03-16 17:42:01 +00:00
|
|
|
# Deletes a key from target file
|
|
|
|
#
|
|
|
|
# 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-02-20 21:24:17 +00:00
|
|
|
#######################
|
|
|
|
# returns FTL's current telnet API port
|
|
|
|
#######################
|
|
|
|
getFTLAPIPort(){
|
2022-03-16 20:30:31 +00:00
|
|
|
local FTLCONFFILE="/etc/pihole/pihole-FTL.conf"
|
|
|
|
local DEFAULT_PORT_FILE="/run/pihole-FTL.port"
|
|
|
|
local DEFAULT_FTL_PORT=4711
|
2022-02-20 21:24:17 +00:00
|
|
|
local PORTFILE
|
|
|
|
local ftl_api_port
|
|
|
|
|
2022-03-16 20:30:31 +00:00
|
|
|
if [ -f "$FTLCONFFILE" ]; then
|
2022-02-20 21:24:17 +00:00
|
|
|
# if PORTFILE is not set in pihole-FTL.conf, use the default path
|
|
|
|
PORTFILE="$( (grep "^PORTFILE=" $FTLCONFFILE || echo "$DEFAULT_PORT_FILE") | cut -d"=" -f2-)"
|
|
|
|
fi
|
|
|
|
|
2022-03-16 20:30:31 +00:00
|
|
|
if [ -s "$PORTFILE" ]; then
|
2022-02-20 21:24:17 +00:00
|
|
|
# -s: FILE exists and has a size greater than zero
|
2022-03-16 20:30:31 +00:00
|
|
|
ftl_api_port=$(cat "${PORTFILE}")
|
2022-02-20 21:24:17 +00:00
|
|
|
# Exploit prevention: unset the variable if there is malicious content
|
2022-03-16 20:30:31 +00:00
|
|
|
# Verify that the value read from the file is numeric
|
|
|
|
expr "$ftl_api_port" : "[^[:digit:]]" > /dev/null && unset ftl_api_port
|
2022-02-20 21:24:17 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# echo the port found in the portfile or default to the default port
|
|
|
|
echo "${ftl_api_port:=$DEFAULT_FTL_PORT}"
|
|
|
|
}
|