install 14 KB

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