install 15 KB

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