install 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. #!/bin/bash
  2. #=================================================
  3. # GENERIC STARTING
  4. #=================================================
  5. # IMPORT GENERIC HELPERS
  6. #=================================================
  7. # Load common variables for all scripts.
  8. source _variables
  9. source _common.sh
  10. source /usr/share/yunohost/helpers
  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. pihole_version="$YNH_APP_ARG_PIHOLE_VERSION"
  25. app=$YNH_APP_INSTANCE_NAME
  26. #=================================================
  27. # CHECK IF THE APP CAN BE INSTALLED WITH THIS ARGS
  28. #=================================================
  29. ynh_script_progression --message="Validating installation parameters..." --weight=2
  30. final_path=/var/www/$app
  31. test ! -e "$final_path" || ynh_die --message="This path already contains a folder"
  32. # Register (book) web path
  33. ynh_webpath_register --app=$app --domain=$domain --path_url=$path_url
  34. #=================================================
  35. # STORE SETTINGS FROM MANIFEST
  36. #=================================================
  37. ynh_script_progression --message="Storing installation settings..." --weight=3
  38. ynh_app_setting_set --app=$app --key=domain --value=$domain
  39. ynh_app_setting_set --app=$app --key=path --value=$path_url
  40. ynh_app_setting_set --app=$app --key=admin --value=$admin
  41. ynh_app_setting_set --app=$app --key=query_logging --value=$query_logging
  42. ynh_app_setting_set --app=$app --key=enable_dhcp --value=$enable_dhcp
  43. ynh_app_setting_set --app=$app --key=pihole_version --value="$pihole_version"
  44. ynh_app_setting_set --app=$app --key=overwrite_setupvars --value=1
  45. ynh_app_setting_set --app=$app --key=overwrite_ftl --value=1
  46. ynh_app_setting_set --app=$app --key=overwrite_nginx --value=1
  47. ynh_app_setting_set --app=$app --key=overwrite_phpfpm --value=1
  48. ynh_app_setting_set --app=$app --key=admin_mail_html --value=1
  49. #=================================================
  50. # STANDARD MODIFICATIONS
  51. #=================================================
  52. # FIND AND OPEN A PORT
  53. #=================================================
  54. ynh_script_progression --message="Finding an available port..." --weight=12
  55. # Find a free port
  56. port=$(ynh_find_port --port=4711)
  57. if [ $port -gt 4720 ]
  58. then
  59. ynh_die --message="The ports 4711 to 4720 are already in use. Pi-hole can't work on another port. Please try to free one of these ports."
  60. fi
  61. ynh_app_setting_set --app=$app --key=port --value=$port
  62. # Disable the port 53 for upnp
  63. ynh_exec_fully_quiet yunohost firewall disallow Both 53 --no-reload
  64. ynh_exec_fully_quiet yunohost firewall allow Both 53 --no-upnp
  65. #=================================================
  66. # INSTALL DEPENDENCIES
  67. #=================================================
  68. ynh_script_progression --message="Installing dependencies..." --weight=12
  69. ynh_install_app_dependencies $app_depencencies
  70. #=================================================
  71. # CREATE DEDICATED USER
  72. #=================================================
  73. ynh_script_progression --message="Configuring system user..." --weight=2
  74. # Create a dedicated system user
  75. ynh_system_user_create --username=$app
  76. #=================================================
  77. # DOWNLOAD, CHECK AND UNPACK SOURCE
  78. #=================================================
  79. ynh_script_progression --message="Setting up source files..." --weight=4
  80. ynh_app_setting_set --app=$app --key=final_path --value=$final_path
  81. # Make a copy of local pihole repository (for Gravity)
  82. pihole_local_repo="/etc/.pihole"
  83. if [ "$pihole_version" == "Last 3.X" ]
  84. then
  85. # Install the version 3.3.1
  86. ynh_setup_source --dest_dir="$pihole_local_repo" --source_id=app_3
  87. # Install admin dashboard
  88. ynh_setup_source --dest_dir="$final_path" --source_id=admin_dashboard_3
  89. else
  90. # Install the last version available
  91. ynh_setup_source --dest_dir="$pihole_local_repo" --source_id=app_last
  92. # Install admin dashboard
  93. ynh_setup_source --dest_dir="$final_path" --source_id=admin_dashboard_last
  94. fi
  95. chown $app:www-data "$final_path"
  96. #=================================================
  97. # NGINX CONFIGURATION
  98. #=================================================
  99. ynh_script_progression --message="Configuring nginx web server..." --weight=2
  100. # Create a dedicated nginx config
  101. ynh_add_nginx_config
  102. #=================================================
  103. # PHP-FPM CONFIGURATION
  104. #=================================================
  105. ynh_script_progression --message="Configuring php-fpm..." --weight=2
  106. # Create a dedicated php-fpm config
  107. ynh_add_fpm_config --usage=low --footprint=low --dedicated_service
  108. #=================================================
  109. # SPECIFIC SETUP
  110. #=================================================
  111. # CREATE DIRECTORIES AND POPULATE THEM
  112. #=================================================
  113. ynh_script_progression --message="Creating and populating directories..."
  114. pihole_storage="/etc/pihole"
  115. mkdir -p "$pihole_storage"
  116. chown $app: -R "$pihole_storage"
  117. pihole_dir="/opt/pihole"
  118. mkdir -p "$pihole_dir"
  119. # Make a copy of Pi-Hole scripts
  120. cp -a "$pihole_local_repo/gravity.sh" "$pihole_dir/"
  121. cp -a $pihole_local_repo/advanced/Scripts/*.sh "$pihole_dir/"
  122. # And copy this fucking COL_TABLE file...
  123. cp -a "$pihole_local_repo/advanced/Scripts/COL_TABLE" "$pihole_dir/"
  124. #=================================================
  125. # COPY PI-HOLE MAIN SCRIPT
  126. #=================================================
  127. ynh_script_progression --message="Copying Pi-Hole main script..."
  128. cp -a "$pihole_local_repo/pihole" /usr/local/bin/
  129. cp -a "$pihole_local_repo/advanced/bash-completion/pihole" /etc/bash_completion.d/pihole
  130. #=================================================
  131. # CREATE LOG FILES
  132. #=================================================
  133. touch /var/log/{pihole,pihole-FTL}.log
  134. chmod 644 /var/log/{pihole,pihole-FTL}.log
  135. dnsmasq_user=$(grep DNSMASQ_USER= /etc/init.d/dnsmasq | cut -d'"' -f2)
  136. chown $dnsmasq_user:root /var/log/{pihole,pihole-FTL}.log
  137. #=================================================
  138. # CREATE SUDOER FILE
  139. #=================================================
  140. # This sudoers config allow pihole to execute /usr/local/bin/pihole as root without password. Nothing more.
  141. if [ "$pihole_version" == "Last 3.X" ]
  142. then
  143. cp "$pihole_local_repo/advanced/pihole.sudo" /etc/sudoers.d/pihole
  144. else
  145. cp "$pihole_local_repo/advanced/Templates/pihole.sudo" /etc/sudoers.d/pihole
  146. fi
  147. echo "$app ALL=NOPASSWD: /usr/local/bin/pihole" >> /etc/sudoers.d/pihole
  148. # echo "Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin" >> /etc/sudoers.d/pihole
  149. chmod 0440 /etc/sudoers.d/pihole
  150. #=================================================
  151. # INSTALL LOGROTATE SCRIPT FOR PI-HOLE
  152. #=================================================
  153. if [ "$pihole_version" == "Last 3.X" ]
  154. then
  155. cp "$pihole_local_repo/advanced/logrotate" "$pihole_storage/logrotate"
  156. else
  157. cp "$pihole_local_repo/advanced/Templates/logrotate" "$pihole_storage/logrotate"
  158. fi
  159. sed -i "/# su #/d;" "$pihole_storage/logrotate"
  160. #=================================================
  161. # INSTALLATION OF PIHOLE-FTL
  162. #=================================================
  163. ynh_script_progression --message="Installing PiHole-FTL..." --weight=30
  164. # Get the source of Pi-Hole-FTL
  165. FTL_temp_path=$(mktemp -d)
  166. if [ "$pihole_version" == "Last 3.X" ]
  167. then
  168. # Install the version 3.3.1
  169. ynh_setup_source --dest_dir="$FTL_temp_path" --source_id=FTL_3
  170. else
  171. # Install the last version available
  172. ynh_setup_source --dest_dir="$FTL_temp_path" --source_id=FTL_last
  173. fi
  174. # Instead of downloading a binary file, we're going to compile it
  175. ( cd "$FTL_temp_path"
  176. if [ "$pihole_version" == "Last available" ]
  177. then
  178. ynh_exec_warn_less cmake .
  179. fi
  180. ynh_exec_warn_less make
  181. ynh_exec_warn_less make install )
  182. ynh_secure_remove --file="$FTL_temp_path"
  183. cp "../conf/dns-servers.conf" "$pihole_storage"
  184. ynh_add_config --template="../conf/pihole-FTL.conf" --destination="$pihole_storage/pihole-FTL.conf"
  185. if [ "$pihole_version" == "Last 3.X" ]
  186. then
  187. # Version 3.3.1
  188. cp -a $pihole_local_repo/advanced/pihole-FTL.service /etc/init.d/pihole-FTL
  189. chmod +x /etc/init.d/pihole-FTL
  190. ynh_exec_warn_less systemctl enable pihole-FTL --quiet
  191. else
  192. # Last version available
  193. # Stopped dnsmasq to replace it by pihole-FTL
  194. ynh_systemd_action --action=stop --service_name=dnsmasq
  195. # Disable the real dnsmasq service
  196. ynh_exec_warn_less systemctl disable dnsmasq --quiet
  197. # And move the files that make the service available in systemd to really disable it
  198. mv /lib/systemd/system/dnsmasq.service /lib/systemd/system/.dnsmasq.service.backup_by_pihole
  199. mv /etc/init.d/dnsmasq /etc/init.d/.dnsmasq.backup_by_pihole
  200. # Move dnsmasq to preserve the current binary
  201. mv /usr/sbin/dnsmasq /usr/sbin/dnsmasq.backup_by_pihole
  202. # Replace dnsmasq by pihole-FTL
  203. # NOTE: pihole-FTL is actually a modified version of dnsmasq
  204. # https://github.com/pi-hole/FTL/tree/master/dnsmasq
  205. ln -s /usr/bin/pihole-FTL /usr/sbin/dnsmasq
  206. cp -a $pihole_local_repo/advanced/Templates/pihole-FTL.service /etc/init.d/pihole-FTL
  207. chmod +x /etc/init.d/pihole-FTL
  208. ynh_exec_warn_less systemctl enable pihole-FTL --quiet
  209. # Replace the service dnsmasq by pihole-FTL
  210. # That way, YunoHost can continue to use dnsmasq by actually using pihole-FTL
  211. ln -s /run/systemd/generator.late/pihole-FTL.service /etc/systemd/system/dnsmasq.service
  212. # Reload systemd config
  213. systemctl daemon-reload
  214. fi
  215. #=================================================
  216. # BUILD VARIABLES FILE
  217. #=================================================
  218. setupVars="$pihole_storage/setupVars.conf"
  219. # Get the default network interface
  220. main_iface=$(ip route | grep --max-count=1 default | awk '{print $5;}')
  221. echo "PIHOLE_INTERFACE=$main_iface" > $setupVars
  222. echo "IPV4_ADDRESS=127.0.0.1" >> $setupVars
  223. echo "IPV6_ADDRESS=::1" >> $setupVars
  224. echo "PIHOLE_DNS_1=" >> $setupVars
  225. echo "PIHOLE_DNS_2=" >> $setupVars
  226. if [ $query_logging -eq 1 ]; then
  227. query_logging=true
  228. else
  229. query_logging=false
  230. fi
  231. echo "QUERY_LOGGING=$query_logging" >> $setupVars
  232. echo "INSTALL_WEB=true" >> $setupVars
  233. # Calculate and store the config file checksum into the app settings
  234. ynh_store_file_checksum --file="$setupVars"
  235. #=================================================
  236. # CONFIGURE DNS FOR THE LOCAL DOMAINS
  237. #=================================================
  238. ynh_script_progression --message="Configuring dns for the local domains..." --weight=7
  239. # Find the IP associated to the network interface
  240. localipv4=$(ip address | grep "${main_iface}\$" | awk '{print $2;}' | cut -d/ -f1)
  241. # List all YunoHost domains
  242. while read perdomain
  243. do
  244. # Comment domain resolution in /etc/hosts on 127.0.0.1, because they can interfere with the local network resolution.
  245. ynh_replace_string --match_string="^127.0.0.1.*$perdomain" --replace_string="#Commented by pihole# &" --target_file=/etc/hosts
  246. # And add a resolution on the local IP instead
  247. grep -q "^$localipv4.*$perdomain" /etc/hosts || \
  248. echo "$localipv4 $perdomain #Added by pihole#" >> /etc/hosts
  249. done <<< "$(yunohost domain list | grep "\." | sed 's/.*: \|.*- //')"
  250. #=================================================
  251. # ENABLE DHCP SERVER
  252. #=================================================
  253. # Open the UDP port 67 for dhcp
  254. ynh_exec_fully_quiet yunohost firewall allow UDP 67 --no-upnp
  255. #=================================================
  256. # SET VERSIONS FOR THE FOOTER OF THE WEB INTERFACE
  257. #=================================================
  258. echo "master master master" > $pihole_storage/localbranches
  259. echo "$pihole_core_version $dashboard_version $FTL_version" | tee $pihole_storage/{GitHubVersions,localversions} > /dev/null
  260. #=================================================
  261. # INSTALL CRON JOB
  262. #=================================================
  263. if [ "$pihole_version" == "Last 3.X" ]
  264. then
  265. cp $pihole_local_repo/advanced/pihole.cron /etc/cron.d/pihole
  266. else
  267. cp $pihole_local_repo/advanced/Templates/pihole.cron /etc/cron.d/pihole
  268. fi
  269. # Remove git usage for version. Which fails because we use here a release instead of master.
  270. ynh_replace_string --match_string=".*updatechecker.*" --replace_string="#&" --target_file=/etc/cron.d/pihole
  271. #=================================================
  272. # RESTART DNSMASQ
  273. #=================================================
  274. # Restart dnsmasq only for the version 3.X, otherwise we're going to restart it twice.
  275. if [ "$pihole_version" == "Last 3.X" ]
  276. then
  277. ynh_script_progression --message="Restarting Dnsmasq..." --weight=2
  278. ynh_systemd_action --action=restart --service_name=dnsmasq
  279. fi
  280. #=================================================
  281. # BUILD THE LISTS WITH GRAVITY
  282. #=================================================
  283. ynh_script_progression --message="Building the lists with Gravity..." --weight=7
  284. if [ "$pihole_version" == "Last 3.X" ]
  285. then
  286. cp "$pihole_local_repo/adlists.default" "$pihole_storage/adlists.default"
  287. else
  288. cp "../conf/adlists.default" "$pihole_storage/adlists.list"
  289. fi
  290. ynh_exec_warn_less /opt/pihole/gravity.sh
  291. #=================================================
  292. # SET UP CONF_REGEN HOOK
  293. #=================================================
  294. cp ../conf/dnsmasq_regenconf_hook /usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app
  295. ynh_replace_string --match_string="__APP__" --replace_string="$app" --target_file="/usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app"
  296. systemctl daemon-reload
  297. ynh_exec_warn_less yunohost tools regen-conf dnsmasq
  298. #=================================================
  299. # START PIHOLE-FTL
  300. #=================================================
  301. ynh_script_progression --message="Restarting PiHole-FTL..." --weight=2
  302. ynh_systemd_action --action=restart --service_name=pihole-FTL
  303. #=================================================
  304. # GENERIC FINALISATION
  305. #=================================================
  306. # ADVERTISE SERVICE IN ADMIN PANEL
  307. #=================================================
  308. yunohost service add pihole-FTL --description="PiHole backend service" --log="/var/log/pihole-FTL.log"
  309. #=================================================
  310. # RESTRAIN THE ACCESS TO THE ADMIN ONLY
  311. #=================================================
  312. ynh_script_progression --message="Restraining the access to the admin only..." --weight=2
  313. ynh_permission_update --permission="main" --add="$admin" --remove="all_users"
  314. #=================================================
  315. # RELOAD NGINX
  316. #=================================================
  317. ynh_script_progression --message="Reloading NGINX web server..." --weight=3
  318. ynh_systemd_action --service_name=nginx --action=reload
  319. #=================================================
  320. # SEND A README FOR THE ADMIN
  321. #=================================================
  322. # Get main domain and buid the url of the admin panel of the app.
  323. admin_panel="https://$(grep portal_domain /etc/ssowat/conf.json | cut -d'"' -f4)/yunohost/admin/#/apps/$app"
  324. if [ $enable_dhcp -eq 1 ]
  325. then
  326. dhcp_alert="You asked to use the internal DHCP server of dnsmasq with PiHole.
  327. 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__
  328. "
  329. else
  330. dhcp_alert=""
  331. fi
  332. 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__.
  333. 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__.
  334. 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
  335. ynh_send_readme_to_admin --app_message="mail_to_send" --recipients="$admin" --type=install
  336. #=================================================
  337. # END OF SCRIPT
  338. #=================================================
  339. ynh_script_progression --message="Installation of $app completed" --last