Skip to content

Commit

Permalink
Update code style
Browse files Browse the repository at this point in the history
  • Loading branch information
jacklul committed Mar 1, 2025
1 parent bbd6ea6 commit 33b5424
Show file tree
Hide file tree
Showing 13 changed files with 323 additions and 317 deletions.
16 changes: 8 additions & 8 deletions .github/variables.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,25 @@ if [ "$GITHUB_EVENT_NAME" = "schedule" ]; then
fi

if [[ "$GITHUB_REF" =~ ^refs/tags/.* ]]; then
VERSION="$GITHUB_REF_NAME"
version="$GITHUB_REF_NAME"
elif [[ "$GITHUB_REF" =~ ^refs/pull/.* ]]; then
VERSION="$GITHUB_REF_NAME-$(date +%s)"
version="$GITHUB_REF_NAME-$(date +%s)"
elif [[ "$GITHUB_REF" =~ ^refs/heads/.* ]]; then
VERSION="$(date +%Y.%m.%d)-$(date +%H%M%S)"
version="$(date +%Y.%m.%d)-$(date +%H%M%S)"

if git rev-parse --is-inside-work-tree &>/dev/null; then
VERSION="${VERSION}-$(git describe --always --abbrev=8)"
version="${version}-$(git describe --always --abbrev=8)"
fi
fi

[ -z "$VERSION" ] && { echo "Package version not set"; exit 1; }
[ -z "$version" ] && { echo "Package version not set"; exit 1; }

# Clean version string
if ! echo "$VERSION" | grep -Eq '^[a-zA-Z0-9_.+-]+$'; then
VERSION="${VERSION//[^a-zA-Z0-9_.+-]/-}"
if ! echo "$version" | grep -Eq '^[a-zA-Z0-9_.+-]+$'; then
version="${version//[^a-zA-Z0-9_.+-]/-}"
fi

echo "VERSION=$VERSION" >> "$GITHUB_OUTPUT"
echo "VERSION=$version" >> "$GITHUB_OUTPUT"

if [[ "$GITHUB_REF" =~ ^refs/tags/.* ]] || [ "$GITHUB_REF_NAME" = "master" ]; then
if [ -z "$CORE_REF" ] || [ -z "$WEB_REF" ] || [ -z "$FTL_REF" ]; then
Expand Down
70 changes: 35 additions & 35 deletions files/CONTROL/postinst
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ else
fi

random_number() {
_BASE=$1
_MAX=$2
_base=$1
_max=$2

bash -c "echo \$(($_BASE + RANDOM % $_MAX))"
bash -c "echo \$(($_base + RANDOM % $_max))"
}

gravitydb_version() {
Expand All @@ -30,10 +30,10 @@ gravitydb_version() {
echo " ${INFO} ${COL_BOLD}Running Pi-hole install/upgrade tasks...${COL_NC}"

# Make links to system files if they don't exist in /opt/etc
[ ! -f /opt/etc/hosts ] && [ -f /etc/hosts ] && ln -s /etc/hosts /opt/etc/hosts && SYMLINKS="$SYMLINKS /opt/etc/hosts"
[ ! -f /opt/etc/ethers ] && [ -f /etc/ethers ] && ln -s /etc/ethers /opt/etc/ethers && SYMLINKS="$SYMLINKS /opt/etc/ethers"
[ ! -f /opt/etc/resolv.conf ] && [ -f /etc/resolv.conf ] && ln -s /etc/resolv.conf /opt/etc/resolv.conf && SYMLINKS="$SYMLINKS /opt/etc/resolv.conf"
[ -n "$SYMLINKS" ] && echo " ${INFO} Created symlinks:$SYMLINKS"
[ ! -f /opt/etc/hosts ] && [ -f /etc/hosts ] && ln -s /etc/hosts /opt/etc/hosts && symlinks="$symlinks /opt/etc/hosts"
[ ! -f /opt/etc/ethers ] && [ -f /etc/ethers ] && ln -s /etc/ethers /opt/etc/ethers && symlinks="$symlinks /opt/etc/ethers"
[ ! -f /opt/etc/resolv.conf ] && [ -f /etc/resolv.conf ] && ln -s /etc/resolv.conf /opt/etc/resolv.conf && symlinks="$symlinks /opt/etc/resolv.conf"
[ -n "$symlinks" ] && echo " ${INFO} Created symlinks:$symlinks"

# Set random gravity update and updatechecker times
if [ -f /opt/etc/cron.d/pihole ] && grep -q "59 1 \|59 17 " /opt/etc/cron.d/pihole; then
Expand All @@ -46,39 +46,39 @@ fi
# Set user and group used for rotating the logs by logrotate
if [ -f /opt/etc/pihole/logrotate ]; then
#shellcheck disable=SC2010
LOG_USER_GROUP="$(ls -al "/opt/var/log" | grep ' \.$' | awk '{print $3 " " $4}')"
log_user_group="$(ls -al "/opt/var/log" | grep ' \.$' | awk '{print $3 " " $4}')"

if [ -n "$LOG_USER_GROUP" ]; then
if [ -n "$log_user_group" ]; then
echo " ${INFO} Setting user and group in logrotate file..."

if grep -q "# su #" /opt/etc/pihole/logrotate; then
sed "s/# su #/su $LOG_USER_GROUP/g;" -i /opt/etc/pihole/logrotate
sed "s/# su #/su $log_user_group/g;" -i /opt/etc/pihole/logrotate
else
sed "s/su.*$/su $LOG_USER_GROUP/g;" -i /opt/etc/pihole/logrotate
sed "s/su.*$/su $log_user_group/g;" -i /opt/etc/pihole/logrotate
fi
fi
fi

# Apply possible workarounds
ROOT_USER="$(id -nu 0 2> /dev/null)"
if [ "$ROOT_USER" != "root" ]; then
root_user="$(id -nu 0 2> /dev/null)"
if [ "$root_user" != "root" ]; then
echo " ${INFO} ${COL_YELLOW}It looks like the root user is not called 'root' - this can cause issues!${COL_NC}"

if ! grep -q "^#*\s*pi-hole-no-crontab-workaround" "/opt/var/spool/cron/crontabs/$ROOT_USER" 2> /dev/null; then
if ! grep -q "^#*\s*pi-hole-no-crontab-workaround" "/opt/var/spool/cron/crontabs/$root_user" 2> /dev/null; then
echo " ${INFO} ${COL_BOLD}Applying crontab workaround...${COL_NC}"

if [ ! -f "/opt/var/spool/cron/crontabs/$ROOT_USER" ]; then
echo " ${INFO} Creating /opt/var/spool/cron/crontabs/$ROOT_USER"
if [ ! -f "/opt/var/spool/cron/crontabs/$root_user" ]; then
echo " ${INFO} Creating /opt/var/spool/cron/crontabs/$root_user"
mkdir -p /opt/var/spool/cron/crontabs
touch "/opt/var/spool/cron/crontabs/$ROOT_USER"
touch "/opt/var/spool/cron/crontabs/$root_user"
else
echo " ${INFO} Modifying /opt/var/spool/cron/crontabs/$ROOT_USER"
echo " ${INFO} Modifying /opt/var/spool/cron/crontabs/$root_user"
fi

SEPARATOR="### Pi-hole crontab - do not modify ###"
sed "/$SEPARATOR/,/$SEPARATOR/d" -i "/opt/var/spool/cron/crontabs/$ROOT_USER"
separator="### Pi-hole crontab - do not modify ###"
sed "/$separator/,/$separator/d" -i "/opt/var/spool/cron/crontabs/$root_user"
{
echo "$SEPARATOR"
echo "$separator"

cat <<EOF
# Everything between the marked lines will be automatically modified by install/upgrade script.
Expand All @@ -87,40 +87,40 @@ EOF

grep -Ev "^#|^\s*$" /opt/etc/cron.d/pihole | sed "s/ root / /g"

echo "$SEPARATOR"
} >> "/opt/var/spool/cron/crontabs/$ROOT_USER"
echo "$separator"
} >> "/opt/var/spool/cron/crontabs/$root_user"

chmod 600 "/opt/var/spool/cron/crontabs/$ROOT_USER"
chmod 600 "/opt/var/spool/cron/crontabs/$root_user"

if [ ! -f /opt/var/spool/cron/cron.allow ] && ! grep -q "$ROOT_USER" /opt/var/spool/cron/cron.allow 2> /dev/null; then
echo " ${INFO} Adding $ROOT_USER to /opt/var/spool/cron/cron.allow"
echo "$ROOT_USER" >> /opt/var/spool/cron/cron.allow
if [ ! -f /opt/var/spool/cron/cron.allow ] && ! grep -q "$root_user" /opt/var/spool/cron/cron.allow 2> /dev/null; then
echo " ${INFO} Adding $root_user to /opt/var/spool/cron/cron.allow"
echo "$root_user" >> /opt/var/spool/cron/cron.allow
fi
fi
fi

# Add a password for the web interface if the config file doesn't exist (new install)
if [ ! -f /opt/etc/pihole/pihole.toml ]; then
# same code as in Pi-hole's basic-install.sh
PASSWORD=$(tr -dc _A-Z-a-z-0-9 </dev/urandom | head -c 8)
pihole setpassword "$PASSWORD" > /dev/null
new_password=$(tr -dc _A-Z-a-z-0-9 </dev/urandom | head -c 8)
pihole setpassword "$new_password" > /dev/null

echo " ${INFO} ${COL_LIGHT_GREEN}Generated password for the web interface: $PASSWORD${COL_NC}"
echo " ${INFO} ${COL_LIGHT_GREEN}Generated password for the web interface: $new_password${COL_NC}"
echo " ${INFO} ${COL_BOLD}It can be changed using 'pihole setpassword' command.${COL_NC}"
elif [ -z "$(pihole-FTL --config webserver.api.pwhash)" ]; then # Show a warning if the password is not set
echo " ${INFO} ${COL_LIGHT_RED}Password for the web interface is not set - this is a security risk!${COL_NC}"
echo " ${INFO} ${COL_BOLD}Please set it using 'pihole setpassword' command.${COL_NC}"
fi

# Gravity database file path
GRAVITYDB_FILE="$(pihole-FTL --config -q files.gravity)"
gravitydb_file="$(pihole-FTL --config -q files.gravity)"

# Upgrade existing gravity database
if [ -n "$GRAVITYDB_FILE" ] && [ -f "$GRAVITYDB_FILE" ]; then
GRAVITYDB_VERSION="$(gravitydb_version "$GRAVITYDB_FILE")"
if [ -n "$gravitydb_file" ] && [ -f "$gravitydb_file" ]; then
gravitydb_version="$(gravitydb_version "$gravitydb_file")"
bash /opt/share/pihole/gravity.sh --upgrade

[ "$(gravitydb_version "$GRAVITYDB_FILE")" != "$GRAVITYDB_VERSION" ] && RUN_GRAVITY=true
[ "$(gravitydb_version "$gravitydb_file")" != "$gravitydb_version" ] && run_gravity=true
fi

# Restart the service if it's already running
Expand All @@ -140,7 +140,7 @@ fi

# Make sure gravity database exists and that
# gravity is rebuilt after database upgrade
if [ -n "$RUN_GRAVITY" ] || { [ -n "$GRAVITYDB_FILE" ] && [ ! -f "$GRAVITYDB_FILE" ] ; }; then
if [ -n "$run_gravity" ] || { [ -n "$gravitydb_file" ] && [ ! -f "$gravitydb_file" ] ; }; then
if ! timeout 3600 bash /opt/share/pihole/gravity.sh --force; then
echo " ${CROSS} ${COL_LIGHT_RED}Gravity database rebuild failed - waited 1 hour for it to complete!${COL_NC}"
fi
Expand Down
70 changes: 35 additions & 35 deletions files/CONTROL/preinst
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,36 @@ PATH="/opt/sbin:/opt/bin:$PATH"
# so we check if they exist and then prompt the user to install them
# These were not added to package dependencies on purpose as they
# should be included in busybox on most systems
#COMMAND_DEPS="" # Format: command or command:package
COREUTILS_DEPS="basename cat chmod chown cp date df dirname echo false head kill ln ls mkdir mv pwd rm tail test touch true uptime"
command_deps="" # Format: command or command:package
coreutils_deps="basename cat chmod chown cp date df dirname echo false head kill ln ls mkdir mv pwd rm tail test touch true uptime"

command_exists() {
_COMMAND=$1
_command=$1

if [ -z "$CHECK_COMMAND" ]; then
type 2> /dev/null && CHECK_COMMAND="type"
which --help > /dev/null 2>&1 && CHECK_COMMAND="which"
whereis --help > /dev/null 2>&1 && CHECK_COMMAND="whereis"
if [ -z "$_check_command" ]; then
type 2> /dev/null && _check_command="type"
which --help > /dev/null 2>&1 && _check_command="which"
whereis --help > /dev/null 2>&1 && _check_command="whereis"

if [ -z "$CHECK_COMMAND" ]; then
if [ -z "$_check_command" ]; then
echo "Unable to check for missing dependencies - 'type', 'which' or 'whereis' command is required!"
exit 1
fi
fi

case $CHECK_COMMAND in
type)
if [ -n "$(type "$_COMMAND" 2>/dev/null | awk '{print $3}')" ]; then
case $_check_command in
"type")
if [ -n "$(type "$_command" 2>/dev/null | awk '{print $3}')" ]; then
return 0
fi
;;
which)
if [ -n "$(which "$_COMMAND" 2>/dev/null)" ]; then
"which")
if [ -n "$(which "$_command" 2>/dev/null)" ]; then
return 0
fi
;;
whereis)
if [ -n "$(whereis -b "$_COMMAND" | cut -d ' ' -f2)" ]; then
"whereis")
if [ -n "$(whereis -b "$_command" | cut -d ' ' -f2)" ]; then
return 0
fi
;;
Expand All @@ -50,43 +50,43 @@ command_exists() {

##########

MISSING_CMDS=""
MISSING_PKGS=""
missing_commands=""
missing_packages=""

for COMMAND in $COMMAND_DEPS; do
if echo "$COMMAND" | grep -q ":"; then
PACKAGE="$(echo "$COMMAND" | cut -d ':' -f 2 2> /dev/null)"
COMMAND="$(echo "$COMMAND" | cut -d ':' -f 1 2> /dev/null)"
for command in $command_deps; do
if echo "$command" | grep -q ":"; then
package="$(echo "$command" | cut -d ':' -f 2 2> /dev/null)"
command="$(echo "$command" | cut -d ':' -f 1 2> /dev/null)"

echo "$COMMAND" | grep -q ":" && continue # no 'cut' command?
echo "$command" | grep -q ":" && continue # no 'cut' command?
else
PACKAGE="$COMMAND"
package="$command"
fi

#shellcheck disable=SC2086
if ! command_exists $COMMAND; then
MISSING_CMDS="$MISSING_CMDS $COMMAND"
MISSING_PKGS="$MISSING_PKGS $PACKAGE"
if ! command_exists $command; then
missing_commands="$missing_commands $command"
missing_packages="$missing_packages $package"
fi
done

for COMMAND in $COREUTILS_DEPS; do
for command in $coreutils_deps; do
#shellcheck disable=SC2086
if ! command_exists $COMMAND; then
MISSING_CMDS="$MISSING_CMDS $COMMAND"
MISSING_PKGS="$MISSING_PKGS coreutils-$COMMAND"
if ! command_exists $command; then
missing_commands="$missing_commands $command"
missing_packages="$missing_packages coreutils-$command"
fi
done

if [ -n "$MISSING_CMDS" ]; then
MISSING_CMDS="$(echo "$MISSING_CMDS" | awk '{$1=$1};1')"
MISSING_PKGS="$(echo "$MISSING_PKGS" | awk '{$1=$1};1')"
if [ -n "$missing_commands" ]; then
missing_commands="$(echo "$missing_commands" | awk '{$1=$1};1')"
missing_packages="$(echo "$missing_packages" | awk '{$1=$1};1')"

cat <<EOT
Your system is missing required commands: $MISSING_CMDS
Your system is missing required commands: $missing_commands
Install them by running the following command:
opkg install $MISSING_PKGS
opkg install $missing_packages
EOT

Expand Down
14 changes: 7 additions & 7 deletions files/opt/etc/init.d/S65pihole-FTL
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ PATH=/opt/sbin:/opt/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:
case $1 in
start | restart)
if [ -z "$ARGS" ] && [ -z "$PREARGS" ]; then
ROOT_USER="$(id -nu 0 2> /dev/null)"
ROOT_GROUP="$(id -ng 0 2> /dev/null)"
root_user="$(id -nu 0 2> /dev/null)"
root_group="$(id -ng 0 2> /dev/null)"

if [ -n "$ROOT_USER" ] && [ -n "$ROOT_GROUP" ]; then # if this is empty then this can indicate absence of 'id' command
if [ -n "$root_user" ] && [ -n "$root_group" ]; then # if this is empty then this can indicate absence of 'id' command
if [ -n "$(id -u pihole 2> /dev/null)" ]; then
ARGS="-- -u pihole -g pihole"

Expand All @@ -31,14 +31,14 @@ case $1 in
if setcap CAP_NET_BIND_SERVICE,CAP_NET_RAW,CAP_NET_ADMIN,CAP_SYS_NICE,CAP_IPC_LOCK,CAP_CHOWN+eip "/opt/bin/pihole-FTL"; then
PREARGS="nonroot pihole"
else
echo "Warning: Starting pihole-FTL as '$ROOT_USER' (then dropping to 'pihole') because setting capabilities is not supported on this system" >&2
echo "Warning: Starting pihole-FTL as '$root_user' (then dropping to 'pihole') because setting capabilities is not supported on this system" >&2
fi
else # start as root user
ARGS="-- -u $ROOT_USER -g $ROOT_GROUP" # workaround for "[dnsmasq] unknown user or group: root" error
ARGS="-- -u $root_user -g $root_group" # workaround for "[dnsmasq] unknown user or group: root" error

echo "Warning: Starting pihole-FTL as '$ROOT_USER' because 'pihole' user does not exist" >&2
echo "Warning: Starting pihole-FTL as '$root_user' because 'pihole' user does not exist" >&2

chown -R "$ROOT_USER":"$ROOT_GROUP" /opt/etc/pihole /opt/var/log/pihole
chown -R "$root_user":"$root_group" /opt/etc/pihole /opt/var/log/pihole
fi
else
echo "Warning: Unable to determine root user" >&2
Expand Down
8 changes: 4 additions & 4 deletions patches/core-002-chown-tweaks.patch
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ index c681782..1443a90 100755
FTL_PID_FILE="$(getFTLConfigValue files.pid)"

+# In Entware user pihole might not exist
+[ -f /opt/etc/opkg.conf ] && [ -z "$(id -u pihole 2> /dev/null)" ] && NO_PIHOLE_USER=y
+[ -f /opt/etc/opkg.conf ] && [ -z "$(id -u pihole 2> /dev/null)" ] && no_pihole_user=y
+
# Ensure that permissions are set so that pihole-FTL can edit all necessary files
# shellcheck disable=SC2174
mkdir -pm 0640 /opt/var/log/pihole
-chown -R pihole:pihole /opt/etc/pihole /opt/var/log/pihole
+[ -z "$NO_PIHOLE_USER" ] && chown -R pihole:pihole /opt/etc/pihole /opt/var/log/pihole
+[ -z "$no_pihole_user" ] && chown -R pihole:pihole /opt/etc/pihole /opt/var/log/pihole
chmod -R 0640 /opt/var/log/pihole
chmod -R 0660 /opt/etc/pihole

Expand All @@ -50,7 +50,7 @@ index c681782..1443a90 100755
-[ -f /opt/var/log/pihole/FTL.log ] || install -m 640 -o pihole -g pihole /dev/null /opt/var/log/pihole/FTL.log
-[ -f /opt/var/log/pihole/pihole.log ] || install -m 640 -o pihole -g pihole /dev/null /opt/var/log/pihole/pihole.log
-[ -f /opt/etc/pihole/dhcp.leases ] || install -m 644 -o pihole -g pihole /dev/null /opt/etc/pihole/dhcp.leases
+if [ -z "$NO_PIHOLE_USER" ]; then
+if [ -z "$no_pihole_user" ]; then
+ [ -f "${FTL_PID_FILE}" ] || install -D -m 644 -o pihole -g pihole /dev/null "${FTL_PID_FILE}"
+ [ -f /opt/var/log/pihole/FTL.log ] || install -m 640 -o pihole -g pihole /dev/null /opt/var/log/pihole/FTL.log
+ [ -f /opt/var/log/pihole/pihole.log ] || install -m 640 -o pihole -g pihole /dev/null /opt/var/log/pihole/pihole.log
Expand Down Expand Up @@ -78,7 +78,7 @@ index 522d92f..bb8f35b 100755
# User and group are set to pihole:pihole
# Permissions are set to 664 (rw-rw-r--)
- chown pihole:pihole "${1}"
+ [ -z "$NO_PIHOLE_USER" ] && chown pihole:pihole "${1}"
+ [ -z "$no_pihole_user" ] && chown pihole:pihole "${1}"
chmod 664 "${1}"

# Ensure the containing directory is group writable
Loading

0 comments on commit 33b5424

Please sign in to comment.