install 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. #!/bin/bash
  2. #=================================================
  3. # GENERIC STARTING
  4. #=================================================
  5. # IMPORT GENERIC HELPERS
  6. #=================================================
  7. source _common.sh
  8. source /usr/share/yunohost/helpers
  9. # Load common variables for all scripts.
  10. source _variables
  11. #=================================================
  12. # MANAGE FAILURE OF THE SCRIPT
  13. #=================================================
  14. # Exit if an error occurs during the execution of the script
  15. ynh_abort_if_errors
  16. #=================================================
  17. # RETRIEVE ARGUMENTS FROM THE MANIFEST
  18. #=================================================
  19. domain=$YNH_APP_ARG_DOMAIN
  20. path_url=$YNH_APP_ARG_PATH
  21. admin=$YNH_APP_ARG_ADMIN
  22. query_logging=$YNH_APP_ARG_QUERY_LOGGING
  23. enable_dhcp=$YNH_APP_ARG_ENABLE_DHCP
  24. app=$YNH_APP_INSTANCE_NAME
  25. #=================================================
  26. # CHECK IF THE APP CAN BE INSTALLED WITH THIS ARGS
  27. #=================================================
  28. final_path=/var/www/$app
  29. test ! -e "$final_path" || ynh_die "This path already contains a folder"
  30. # Normalize the url path syntax
  31. path_url=$(ynh_normalize_url_path $path_url)
  32. # Register (book) web path
  33. ynh_webpath_register $app $domain $path_url
  34. #=================================================
  35. # STORE SETTINGS FROM MANIFEST
  36. #=================================================
  37. ynh_app_setting_set $app domain $domain
  38. ynh_app_setting_set $app path $path_url
  39. ynh_app_setting_set $app admin $admin
  40. ynh_app_setting_set $app query_logging $query_logging
  41. ynh_app_setting_set $app enable_dhcp $enable_dhcp
  42. ynh_app_setting_set $app overwrite_setupvars "1"
  43. ynh_app_setting_set $app overwrite_ftl "1"
  44. ynh_app_setting_set $app overwrite_nginx "1"
  45. ynh_app_setting_set $app overwrite_phpfpm "1"
  46. #=================================================
  47. # STANDARD MODIFICATIONS
  48. #=================================================
  49. # FIND AND OPEN A PORT
  50. #=================================================
  51. # Find a free port
  52. port=$(ynh_find_port 4711)
  53. if [ $port -gt 4720 ]
  54. then
  55. ynh_die "The ports 4711 to 4720 are already in use. Pi-hole can't works on another port. Please try to free one of this ports."
  56. fi
  57. # Open this port
  58. ynh_exec_fully_quiet yunohost firewall allow --no-upnp TCP $port
  59. ynh_app_setting_set $app port $port
  60. # Disable the port 53 for upnp
  61. ynh_exec_fully_quiet yunohost firewall disallow Both 53 --no-reload
  62. ynh_exec_fully_quiet yunohost firewall allow Both 53 --no-upnp
  63. #=================================================
  64. # INSTALL DEPENDENCIES
  65. #=================================================
  66. ynh_install_app_dependencies $app_depencencies
  67. #=================================================
  68. # DOWNLOAD, CHECK AND UNPACK SOURCE
  69. #=================================================
  70. ynh_app_setting_set $app final_path $final_path
  71. # Make a copy of local pihole repository (for Gravity)
  72. pihole_local_repo="/etc/.pihole"
  73. ynh_setup_source "$pihole_local_repo"
  74. # Install admin dashboard
  75. ynh_setup_source "$final_path" admin_dashboard
  76. #=================================================
  77. # NGINX CONFIGURATION
  78. #=================================================
  79. # Create a dedicated nginx config
  80. ynh_add_nginx_config
  81. #=================================================
  82. # CREATE DEDICATED USER
  83. #=================================================
  84. # Create a dedicated system user
  85. ynh_system_user_create $app
  86. #=================================================
  87. # PHP-FPM CONFIGURATION
  88. #=================================================
  89. # Create a dedicated php-fpm config
  90. ynh_add_fpm_config
  91. #=================================================
  92. # SPECIFIC SETUP
  93. #=================================================
  94. # CREATE DIRECTORIES AND POPULATE THEM
  95. #=================================================
  96. pihole_storage="/etc/pihole"
  97. mkdir -p "$pihole_storage"
  98. chown $app: -R "$pihole_storage"
  99. pihole_dir="/opt/pihole"
  100. mkdir -p "$pihole_dir"
  101. # Make a copy of Pi-Hole scripts
  102. cp -a "$pihole_local_repo/gravity.sh" "$pihole_dir/"
  103. cp -a $pihole_local_repo/advanced/Scripts/*.sh "$pihole_dir/"
  104. # And copy this fucking COL_TABLE file...
  105. cp -a "$pihole_local_repo/advanced/Scripts/COL_TABLE" "$pihole_dir/"
  106. #=================================================
  107. # COPY PI-HOLE MAIN SCRIPT
  108. #=================================================
  109. cp -a "$pihole_local_repo/pihole" /usr/local/bin/
  110. cp -a "$pihole_local_repo/advanced/bash-completion/pihole" /etc/bash_completion.d/pihole
  111. #=================================================
  112. # CREATE LOG FILES
  113. #=================================================
  114. touch /var/log/pihole.log
  115. chmod 644 /var/log/pihole.log
  116. dnsmasq_user=$(grep DNSMASQ_USER= /etc/init.d/dnsmasq | cut -d'"' -f2)
  117. chown $dnsmasq_user:root /var/log/pihole.log
  118. #=================================================
  119. # CREATE SUDOER FILE
  120. #=================================================
  121. # This sudoers config allow pihole to execute /usr/local/bin/pihole as root without password. Nothing more.
  122. cp "$pihole_local_repo/advanced/pihole.sudo" /etc/sudoers.d/pihole
  123. echo "$app ALL=NOPASSWD: /usr/local/bin/pihole" >> /etc/sudoers.d/pihole
  124. # echo "Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin" >> /etc/sudoers.d/pihole
  125. chmod 0440 /etc/sudoers.d/pihole
  126. #=================================================
  127. # INSTALL LOGROTATE SCRIPT FOR PI-HOLE
  128. #=================================================
  129. cp "$pihole_local_repo/advanced/logrotate" "$pihole_storage/logrotate"
  130. sed -i "/# su #/d;" "$pihole_storage/logrotate"
  131. #=================================================
  132. # INSTALLATION OF PIHOLE-FTL
  133. #=================================================
  134. # Get the source of Pi-Hole-FTL
  135. FTL_temp_path=$(mktemp -d)
  136. ynh_setup_source "$FTL_temp_path" FTL
  137. # Instead of downloading a binary file, we're going to compile it
  138. ( cd "$FTL_temp_path"
  139. ynh_exec_warn_less make
  140. ynh_exec_warn_less make install )
  141. ynh_secure_remove "$FTL_temp_path"
  142. cp "../conf/pihole-FTL.conf" "$pihole_storage"
  143. # Calculate and store the config file checksum into the app settings
  144. ynh_store_file_checksum "$pihole_storage/pihole-FTL.conf"
  145. cp -a $pihole_local_repo/advanced/pihole-FTL.service /etc/init.d/pihole-FTL
  146. chmod +x /etc/init.d/pihole-FTL
  147. ynh_exec_warn_less systemctl enable pihole-FTL
  148. #=================================================
  149. # BUILD VARIABLES FILE
  150. #=================================================
  151. setupVars="$pihole_storage/setupVars.conf"
  152. # Get the default network interface
  153. main_iface=$(ip route | grep default | awk '{print $5;}')
  154. echo "PIHOLE_INTERFACE=$main_iface" > $setupVars
  155. echo "IPV4_ADDRESS=127.0.0.1" >> $setupVars
  156. echo "IPV6_ADDRESS=" >> $setupVars
  157. echo "PIHOLE_DNS_1=" >> $setupVars
  158. echo "PIHOLE_DNS_2=" >> $setupVars
  159. if [ $query_logging -eq 1 ]; then
  160. query_logging=true
  161. else
  162. query_logging=false
  163. fi
  164. echo "QUERY_LOGGING=$query_logging" >> $setupVars
  165. echo "INSTALL_WEB=true" >> $setupVars
  166. # Calculate and store the config file checksum into the app settings
  167. ynh_store_file_checksum "$setupVars"
  168. #=================================================
  169. # SET UP DNSMASQ CONFIG
  170. #=================================================
  171. ynh_systemd_action --action=stop --service_name=dnsmasq
  172. pihole_dnsmasq_config="/etc/dnsmasq.d/01-pihole.conf"
  173. cp "$pihole_local_repo/advanced/01-pihole.conf" $pihole_dnsmasq_config
  174. # Use dns from /etc/resolv.dnsmasq.conf
  175. ynh_replace_string "@DNS1@" "" $pihole_dnsmasq_config
  176. ynh_replace_string "@DNS2@" "" $pihole_dnsmasq_config
  177. ynh_replace_string "^no-resolv" "#no-resolv" $pihole_dnsmasq_config
  178. ynh_replace_string "@INT@" "$main_iface" $pihole_dnsmasq_config
  179. if [ "$query_logging" = "true" ]; then
  180. ynh_replace_string "^#log-queries" "log-queries" $pihole_dnsmasq_config
  181. else
  182. ynh_replace_string "^log-queries" "#log-queries" $pihole_dnsmasq_config
  183. fi
  184. # Fix a too recent option for our dnsmasq version.
  185. ynh_replace_string "log-queries=extra" "log-queries" $pihole_dnsmasq_config
  186. # Calculate and store the config file checksum into the app settings
  187. ynh_store_file_checksum "$pihole_dnsmasq_config"
  188. # To prevent any conflict with the original dnsmasq config, comment cache-size in the original config.
  189. ynh_replace_string "^cache-size=" "#pihole# cache-size=" /etc/dnsmasq.conf
  190. #=================================================
  191. # CONFIGURE DNS FOR THE LOCAL DOMAINS
  192. #=================================================
  193. # Find the IP associated to the network interface
  194. localipv4=$(ip address | grep "${main_iface}\$" | awk '{print $2;}' | cut -d/ -f1)
  195. # List all YunoHost domains
  196. while read perdomain
  197. do
  198. # Comment domain resolution in /etc/hosts on 127.0.0.1, because they can interfere with the local network resolution.
  199. ynh_replace_string "^127.0.0.1.*$perdomain" "#Commented by pihole# &" /etc/hosts
  200. # And add a resolution on the local IP instead
  201. grep -q "^$localipv4.*$perdomain" /etc/hosts || \
  202. echo "$localipv4 $perdomain #Added by pihole#" >> /etc/hosts
  203. done <<< "$(yunohost domain list | grep "\." | sed 's/.*: \|.*- //')"
  204. #=================================================
  205. # ENABLE DHCP SERVER
  206. #=================================================
  207. if [ $enable_dhcp -eq 1 ]
  208. then
  209. max_dhcp_range=250
  210. dhcp_range=100
  211. # Define the dhcp range from the current ip
  212. ip_beginning_part=$(echo "$localipv4" | cut -d. -f1-3)
  213. ip_fourth_part=$(echo "$localipv4" | cut -d. -f4)
  214. b_range=$(( $ip_fourth_part + $dhcp_range ))
  215. if [ $b_range -gt $max_dhcp_range ]; then
  216. b_range=$max_dhcp_range
  217. fi
  218. a_range=$(( $b_range - $dhcp_range ))
  219. # Get the gateway
  220. gateway=$(ip route | grep default | awk '{print $3;}')
  221. # And the mac adress
  222. hw_adress=$(ip link | grep -A1 "$main_iface" | tail -n1 | awk '{print $2;}')
  223. # Copy the config file
  224. cp "../conf/02-pihole-dhcp.conf" "/etc/dnsmasq.d/"
  225. # And set the config
  226. ynh_replace_string "__A_RANGE__" "$ip_beginning_part.$a_range" "/etc/dnsmasq.d/02-pihole-dhcp.conf"
  227. ynh_replace_string "__B_RANGE__" "$ip_beginning_part.$b_range" "/etc/dnsmasq.d/02-pihole-dhcp.conf"
  228. ynh_replace_string "__GATEWAY__" "$gateway" "/etc/dnsmasq.d/02-pihole-dhcp.conf"
  229. # Set a static ip for the server.
  230. echo "dhcp-host=$hw_adress,$localipv4" > "/etc/dnsmasq.d/04-pihole-static-dhcp.conf"
  231. fi
  232. # Open the UDP port 67 for dhcp
  233. ynh_exec_fully_quiet yunohost firewall allow UDP 67 --no-upnp
  234. #=================================================
  235. # RESTART DNSMASQ
  236. #=================================================
  237. ynh_systemd_action --action=restart --service_name=dnsmasq
  238. #=================================================
  239. # INSTALL CRON JOB
  240. #=================================================
  241. cp $pihole_local_repo/advanced/pihole.cron /etc/cron.d/pihole
  242. # Remove git usage for version. Which fails because we use here a release instead of master.
  243. ynh_replace_string ".*updatechecker.*" "#&" /etc/cron.d/pihole
  244. #=================================================
  245. # BUILD THE LISTS WITH GRAVITY
  246. #=================================================
  247. cp "$pihole_local_repo/adlists.default" "$pihole_storage/adlists.default"
  248. ynh_exec_warn_less /opt/pihole/gravity.sh
  249. #=================================================
  250. # START PIHOLE-FTL
  251. #=================================================
  252. ynh_systemd_action --action=restart --service_name=pihole-FTL
  253. #=================================================
  254. # SET UP CONF_REGEN HOOK
  255. #=================================================
  256. cp ../conf/dnsmasq_regenconf_hook /usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app
  257. #=================================================
  258. # GENERIC FINALISATION
  259. #=================================================
  260. # ADVERTISE SERVICE IN ADMIN PANEL
  261. #=================================================
  262. yunohost service add pihole-FTL --log "/var/log/pihole-FTL.log"
  263. #=================================================
  264. # RESTRAIN THE ACCESS TO THE ADMIN ONLY
  265. #=================================================
  266. yunohost app addaccess --users=$admin $app
  267. #=================================================
  268. # RELOAD NGINX
  269. #=================================================
  270. ynh_systemd_action --action=reload --service_name=nginx
  271. #=================================================
  272. # SEND A README FOR THE ADMIN
  273. #=================================================
  274. if [ $enable_dhcp -eq 1 ]
  275. then
  276. dhcp_alert="You asked to use the internal DHCP server of dnsmasq with PiHole.
  277. You should really read the documentation about that, https://github.com/YunoHost-Apps/pihole_ynh/blob/master/dhcp.md
  278. "
  279. else
  280. dhcp_alert=""
  281. fi
  282. message="${dhcp_alert}If you're facing an issue or want to improve this app, please open a new issue in this project: https://github.com/YunoHost-Apps/pihole_ynh"
  283. ynh_send_readme_to_admin --app_message="$message" --recipients="$admin"