|
| 1 | +#!/usr/bin/env bash |
| 2 | +export LC_ALL=C |
| 3 | + |
| 4 | +basename="pihole" |
| 5 | +PIHOLE_COMMAND="/usr/local/bin/${basename}" |
| 6 | + |
| 7 | +piholeDir="/etc/${basename}" |
| 8 | + |
| 9 | +adListFile="${piholeDir}/adlists.list" |
| 10 | + |
| 11 | +domainsExtension="domains" |
| 12 | + |
| 13 | +# Set up tmp dir variable in case it's not configured |
| 14 | +: "${GRAVITY_TMPDIR:=/tmp}" |
| 15 | + |
| 16 | +if [ ! -d "${GRAVITY_TMPDIR}" ] || [ ! -w "${GRAVITY_TMPDIR}" ]; then |
| 17 | + echo -e " ${COL_LIGHT_RED}Gravity temporary directory does not exist or is not a writeable directory, falling back to /tmp. ${COL_NC}" |
| 18 | + GRAVITY_TMPDIR="/tmp" |
| 19 | +fi |
| 20 | + |
| 21 | +gravityDBfile_default="${piholeDir}/gravity.db" |
| 22 | +GRAVITYDB="${gravityDBfile_default}" |
| 23 | + |
| 24 | +# Set this only after sourcing pihole-FTL.conf as the gravity database path may |
| 25 | +# have changed |
| 26 | +gravityDBfile="${GRAVITYDB}" |
| 27 | +gravityTEMPfile="${GRAVITYDB}_temp" |
| 28 | +gravityDIR="$(dirname -- "${gravityDBfile}")" |
| 29 | +gravityOLDfile="${gravityDIR}/gravity_old.db" |
| 30 | + |
| 31 | + |
| 32 | +configMap_adlists() { |
| 33 | + echo " [i] Deleting existing adlists from gravity" |
| 34 | + |
| 35 | + # Experimental feature to clean out domains from gravity to allow a kubernetes ConfigMap to manage them |
| 36 | + pihole-FTL sqlite3 -ni "${gravityDBfile}" "DELETE FROM gravity;" |
| 37 | + pihole-FTL sqlite3 -ni "${gravityDBfile}" "DELETE FROM adlist;" |
| 38 | + pihole-FTL sqlite3 -ni "${gravityDBfile}" "DELETE FROM adlist_by_group;" |
| 39 | + |
| 40 | + echo " [i] Finished clearing out adlists from gravity" |
| 41 | + |
| 42 | + # Migrate list files to new database |
| 43 | + if [ -e "${adListFile}" ]; then |
| 44 | + # Store adlist domains in database |
| 45 | + echo -e " ${INFO} Migrating content of ${adListFile} into new database" |
| 46 | + database_table_from_file "adlist" "${adListFile}" |
| 47 | + fi |
| 48 | + |
| 49 | +} |
| 50 | + |
| 51 | +# Import domains from file and store them in the specified database table |
| 52 | +database_table_from_file() { |
| 53 | + # Define locals |
| 54 | + local table src backup_path backup_file tmpFile list_type |
| 55 | + table="${1}" |
| 56 | + src="${2}" |
| 57 | + backup_path="${piholeDir}/migration_backup" |
| 58 | + backup_file="${backup_path}/$(basename "${2}")" |
| 59 | + # Create a temporary file. We don't use '--suffix' here because not all |
| 60 | + # implementations of mktemp support it, e.g. on Alpine |
| 61 | + tmpFile="$(mktemp -p "${GRAVITY_TMPDIR}")" |
| 62 | + mv "${tmpFile}" "${tmpFile%.*}.gravity" |
| 63 | + |
| 64 | + local timestamp |
| 65 | + timestamp="$(date --utc +'%s')" |
| 66 | + |
| 67 | + local rowid |
| 68 | + declare -i rowid |
| 69 | + rowid=1 |
| 70 | + |
| 71 | + # Special handling for domains to be imported into the common domainlist table |
| 72 | + if [[ "${table}" == "whitelist" ]]; then |
| 73 | + list_type="0" |
| 74 | + table="domainlist" |
| 75 | + elif [[ "${table}" == "blacklist" ]]; then |
| 76 | + list_type="1" |
| 77 | + table="domainlist" |
| 78 | + elif [[ "${table}" == "regex" ]]; then |
| 79 | + list_type="3" |
| 80 | + table="domainlist" |
| 81 | + fi |
| 82 | + |
| 83 | + # Get MAX(id) from domainlist when INSERTing into this table |
| 84 | + if [[ "${table}" == "domainlist" ]]; then |
| 85 | + rowid="$(pihole-FTL sqlite3 -ni "${gravityDBfile}" "SELECT MAX(id) FROM domainlist;")" |
| 86 | + if [[ -z "$rowid" ]]; then |
| 87 | + rowid=0 |
| 88 | + fi |
| 89 | + rowid+=1 |
| 90 | + fi |
| 91 | + |
| 92 | + # Loop over all domains in ${src} file |
| 93 | + # Read file line by line |
| 94 | + grep -v '^ *#' < "${src}" | while IFS= read -r domain |
| 95 | + do |
| 96 | + # Only add non-empty lines |
| 97 | + if [[ -n "${domain}" ]]; then |
| 98 | + if [[ "${table}" == "domain_audit" ]]; then |
| 99 | + # domain_audit table format (no enable or modified fields) |
| 100 | + echo "${rowid},\"${domain}\",${timestamp}" >> "${tmpFile}" |
| 101 | + elif [[ "${table}" == "adlist" ]]; then |
| 102 | + # Adlist table format |
| 103 | + echo "${rowid},\"${domain}\",1,${timestamp},${timestamp},\"Migrated from ${src}\",,0,0,0" >> "${tmpFile}" |
| 104 | + else |
| 105 | + # White-, black-, and regexlist table format |
| 106 | + echo "${rowid},${list_type},\"${domain}\",1,${timestamp},${timestamp},\"Migrated from ${src}\"" >> "${tmpFile}" |
| 107 | + fi |
| 108 | + rowid+=1 |
| 109 | + fi |
| 110 | + done |
| 111 | + |
| 112 | + # Store domains in database table specified by ${table} |
| 113 | + # Use printf as .mode and .import need to be on separate lines |
| 114 | + # see https://unix.stackexchange.com/a/445615/83260 |
| 115 | + output=$( { printf ".timeout 30000\\n.mode csv\\n.import \"%s\" %s\\n" "${tmpFile}" "${table}" | pihole-FTL sqlite3 -ni "${gravityDBfile}"; } 2>&1 ) |
| 116 | + status="$?" |
| 117 | + |
| 118 | + if [[ "${status}" -ne 0 ]]; then |
| 119 | + echo -e "\\n ${CROSS} Unable to fill table ${table}${list_type} in database ${gravityDBfile}\\n ${output}" |
| 120 | + gravity_Cleanup "error" |
| 121 | + fi |
| 122 | + |
| 123 | + # Move source file to backup directory, create directory if not existing |
| 124 | + mkdir -p "${backup_path}" |
| 125 | + mv "${src}" "${backup_file}" 2> /dev/null || \ |
| 126 | + echo -e " ${CROSS} Unable to backup ${src} to ${backup_path}" |
| 127 | + |
| 128 | + # Delete tmpFile |
| 129 | + rm "${tmpFile}" > /dev/null 2>&1 || \ |
| 130 | + echo -e " ${CROSS} Unable to remove ${tmpFile}" |
| 131 | +} |
| 132 | + |
| 133 | +# Clean up after Gravity upon exit or cancellation |
| 134 | +gravity_Cleanup() { |
| 135 | + local error="${1:-}" |
| 136 | + |
| 137 | + str="Cleaning up stray matter" |
| 138 | + echo -ne " ${INFO} ${str}..." |
| 139 | + |
| 140 | + # Delete tmp content generated by Gravity |
| 141 | + rm ${piholeDir}/pihole.*.txt 2> /dev/null |
| 142 | + rm ${piholeDir}/*.tmp 2> /dev/null |
| 143 | + # listCurlBuffer location |
| 144 | + rm "${GRAVITY_TMPDIR}"/*.phgpb 2> /dev/null |
| 145 | + # invalid_domains location |
| 146 | + rm "${GRAVITY_TMPDIR}"/*.ph-non-domains 2> /dev/null |
| 147 | + |
| 148 | + # Ensure this function only runs when gravity_SetDownloadOptions() has completed |
| 149 | + if [[ "${gravity_Blackbody:-}" == true ]]; then |
| 150 | + # Remove any unused .domains files |
| 151 | + for file in "${piholeDir}"/*."${domainsExtension}"; do |
| 152 | + # If list is not in active array, then remove it |
| 153 | + if [[ ! "${activeDomains[*]}" == *"${file}"* ]]; then |
| 154 | + rm -f "${file}" 2> /dev/null || \ |
| 155 | + echo -e " ${CROSS} Failed to remove ${file##*/}" |
| 156 | + fi |
| 157 | + done |
| 158 | + fi |
| 159 | + |
| 160 | + echo -e "${OVER} ${TICK} ${str}" |
| 161 | + |
| 162 | + # Only restart DNS service if offline |
| 163 | + if ! pgrep pihole-FTL &> /dev/null; then |
| 164 | + "${PIHOLE_COMMAND}" restartdns |
| 165 | + dnsWasOffline=true |
| 166 | + fi |
| 167 | + |
| 168 | + # Print Pi-hole status if an error occurred |
| 169 | + if [[ -n "${error}" ]]; then |
| 170 | + "${PIHOLE_COMMAND}" status |
| 171 | + exit 1 |
| 172 | + fi |
| 173 | +} |
0 commit comments