install 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. ynh_abort_if_errors # Active trap pour arrêter le script si une erreur est détectée.
  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. final_path=/var/www/$app
  28. test ! -e "$final_path" || ynh_die "This path already contains a folder"
  29. # Normalize the url path syntax
  30. path_url=$(ynh_normalize_url_path $path_url)
  31. # Register (book) web path
  32. ynh_webpath_register $app $domain $path_url
  33. #=================================================
  34. # STORE SETTINGS FROM MANIFEST
  35. #=================================================
  36. ynh_app_setting_set $app domain $domain
  37. ynh_app_setting_set $app path $path_url
  38. ynh_app_setting_set $app admin $admin
  39. ynh_app_setting_set $app query_logging $query_logging
  40. ynh_app_setting_set $app enable_dhcp $enable_dhcp
  41. ynh_app_setting_set $app overwrite_setupvars "1"
  42. ynh_app_setting_set $app overwrite_ftl "1"
  43. ynh_app_setting_set $app overwrite_nginx "1"
  44. ynh_app_setting_set $app overwrite_phpfpm "1"
  45. #=================================================
  46. # STANDARD MODIFICATIONS
  47. #=================================================
  48. # FIND AND OPEN A PORT
  49. #=================================================
  50. port=$(ynh_find_port 4711) # Cherche un port libre.
  51. if [ $port -gt 4720 ]
  52. then
  53. 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."
  54. fi
  55. # Ouvre le port dans le firewall
  56. ynh_exec_fully_quiet yunohost firewall allow --no-upnp TCP $port
  57. ynh_app_setting_set $app port $port
  58. # Désactive le port 53 en upnp
  59. ynh_exec_fully_quiet yunohost firewall disallow Both 53 --no-reload
  60. ynh_exec_fully_quiet yunohost firewall allow Both 53 --no-upnp
  61. #=================================================
  62. # INSTALL DEPENDENCIES
  63. #=================================================
  64. ynh_install_app_dependencies $app_depencencies
  65. #=================================================
  66. # DOWNLOAD, CHECK AND UNPACK SOURCE
  67. #=================================================
  68. ynh_app_setting_set $app final_path $final_path
  69. # Créer une copie du repo de pihole (nécessaire pour Gravity)
  70. pihole_local_repo="/etc/.pihole"
  71. ynh_setup_source "$pihole_local_repo"
  72. # Installe le dashboard admin
  73. ynh_setup_source "$final_path" admin_dashboard
  74. #=================================================
  75. # NGINX CONFIGURATION
  76. #=================================================
  77. ynh_add_nginx_config
  78. #=================================================
  79. # CREATE DEDICATED USER
  80. #=================================================
  81. ynh_system_user_create $app # Créer un utilisateur système dédié à l'app
  82. #=================================================
  83. # PHP-FPM CONFIGURATION
  84. #=================================================
  85. ynh_add_fpm_config # Créer le fichier de configuration du pool php-fpm et le configure.
  86. #=================================================
  87. # SPECIFIC SETUP
  88. #=================================================
  89. # CREATE THE DIRECTORIES AND POPULATE THEM
  90. #=================================================
  91. pihole_storage="/etc/pihole"
  92. mkdir -p "$pihole_storage"
  93. chown $app: -R "$pihole_storage"
  94. pihole_dir="/opt/pihole"
  95. mkdir -p "$pihole_dir"
  96. # Copie les scripts de Pi-hole
  97. cp -a "$pihole_local_repo/gravity.sh" "$pihole_dir/"
  98. cp -a $pihole_local_repo/advanced/Scripts/*.sh "$pihole_dir/"
  99. # And copy this fucking COL_TABLE file...
  100. cp -a "$pihole_local_repo/advanced/Scripts/COL_TABLE" "$pihole_dir/"
  101. #=================================================
  102. # COPY THE PI-HOLE MAIN SCRIPT
  103. #=================================================
  104. cp -a "$pihole_local_repo/pihole" /usr/local/bin/
  105. cp -a "$pihole_local_repo/advanced/bash-completion/pihole" /etc/bash_completion.d/pihole
  106. #=================================================
  107. # CREATE LOG FILES
  108. #=================================================
  109. touch /var/log/pihole.log
  110. chmod 644 /var/log/pihole.log
  111. dnsmasq_user=$(grep DNSMASQ_USER= /etc/init.d/dnsmasq | cut -d'"' -f2)
  112. chown $dnsmasq_user:root /var/log/pihole.log
  113. #=================================================
  114. # CREATE SUDOER FILE
  115. #=================================================
  116. # Cette configuration sudoers autorise pihole à exécuter /usr/local/bin/pihole en root sans mot de passe. Pas plus.
  117. cp "$pihole_local_repo/advanced/pihole.sudo" /etc/sudoers.d/pihole
  118. echo "$app ALL=NOPASSWD: /usr/local/bin/pihole" >> /etc/sudoers.d/pihole
  119. # echo "Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin" >> /etc/sudoers.d/pihole
  120. chmod 0440 /etc/sudoers.d/pihole
  121. #=================================================
  122. # INSTALL LOGROTATE SCRIPT FOR PI-HOLE
  123. #=================================================
  124. cp "$pihole_local_repo/advanced/logrotate" "$pihole_storage/logrotate"
  125. sed -i "/# su #/d;" "$pihole_storage/logrotate"
  126. #=================================================
  127. # INSTALLATION OF PIHOLE-FTL
  128. #=================================================
  129. # Get the source of Pi-Hole-FTL
  130. FTL_temp_path=$(mktemp -d)
  131. ynh_setup_source "$FTL_temp_path" FTL
  132. # Plutôt que télécharger le binaire C, on le compile nous-même.
  133. ( cd "$FTL_temp_path"
  134. ynh_exec_warn_less make
  135. ynh_exec_warn_less make install )
  136. ynh_secure_remove "$FTL_temp_path"
  137. cp "../conf/pihole-FTL.conf" "$pihole_storage"
  138. ynh_store_file_checksum "$pihole_storage/pihole-FTL.conf" # Enregistre la somme de contrôle du fichier de config
  139. cp -a $pihole_local_repo/advanced/pihole-FTL.service /etc/init.d/pihole-FTL
  140. chmod +x /etc/init.d/pihole-FTL
  141. ynh_exec_warn_less systemctl enable pihole-FTL
  142. #=================================================
  143. # BUILD THE VARIABLES FILE
  144. #=================================================
  145. setupVars="$pihole_storage/setupVars.conf"
  146. # Trouve l'interface réseau par défaut
  147. main_iface=$(ip route | grep default | awk '{print $5;}')
  148. echo "PIHOLE_INTERFACE=$main_iface" > $setupVars
  149. echo "IPV4_ADDRESS=127.0.0.1" >> $setupVars
  150. echo "IPV6_ADDRESS=" >> $setupVars
  151. echo "PIHOLE_DNS_1=" >> $setupVars
  152. echo "PIHOLE_DNS_2=" >> $setupVars
  153. if [ $query_logging -eq 1 ]; then
  154. query_logging=true
  155. else
  156. query_logging=false
  157. fi
  158. echo "QUERY_LOGGING=$query_logging" >> $setupVars
  159. echo "INSTALL_WEB=true" >> $setupVars
  160. ynh_store_file_checksum "$setupVars" # Enregistre la somme de contrôle du fichier de config
  161. #=================================================
  162. # SET UP THE DNSMASQ CONFIG
  163. #=================================================
  164. ynh_systemd_action --action=stop --service_name=dnsmasq
  165. pihole_dnsmasq_config="/etc/dnsmasq.d/01-pihole.conf"
  166. cp "$pihole_local_repo/advanced/01-pihole.conf" $pihole_dnsmasq_config
  167. # On utilise les dns de /etc/resolv.dnsmasq.conf
  168. ynh_replace_string "@DNS1@" "" $pihole_dnsmasq_config
  169. ynh_replace_string "@DNS2@" "" $pihole_dnsmasq_config
  170. ynh_replace_string "^no-resolv" "#no-resolv" $pihole_dnsmasq_config
  171. ynh_replace_string "@INT@" "$main_iface" $pihole_dnsmasq_config
  172. if [ "$query_logging" = "true" ]; then
  173. ynh_replace_string "^#log-queries" "log-queries" $pihole_dnsmasq_config
  174. else
  175. ynh_replace_string "^log-queries" "#log-queries" $pihole_dnsmasq_config
  176. fi
  177. # Fix a too recent option for our dnsmasq version.
  178. ynh_replace_string "log-queries=extra" "log-queries" $pihole_dnsmasq_config
  179. ynh_store_file_checksum "$pihole_dnsmasq_config" # Enregistre la somme de contrôle du fichier de config
  180. # Pour éviter un conflit entre les config de dnsmasq, il faut commenter cache-size dans la config par défaut.
  181. ynh_replace_string "^cache-size=" "#pihole# cache-size=" /etc/dnsmasq.conf
  182. #=================================================
  183. # CONFIGURE DNS FOR THE LOCAL DOMAINS
  184. #=================================================
  185. # Trouve l'ipv4 associée à l'interface trouvée
  186. localipv4=$(ip address | grep "${main_iface}\$" | awk '{print $2;}' | cut -d/ -f1)
  187. # Liste les domaines de yunohost
  188. while read perdomain
  189. do
  190. # Commente les résolutions du domaine sur 127.0.0.1, qui risquerait de bloquer la résolution sur le réseau local
  191. ynh_replace_string "^127.0.0.1.*$perdomain" "#Commented by pihole# &" /etc/hosts
  192. # Et ajoute une résolution sur l'ip local à la place, si elle n'existe pas déjà
  193. grep -q "^$localipv4.*$perdomain" /etc/hosts || \
  194. echo "$localipv4 $perdomain #Added by pihole#" >> /etc/hosts
  195. done <<< "$(yunohost domain list | grep "\." | sed 's/.*: \|.*- //')"
  196. #=================================================
  197. # ENABLE DHCP SERVER
  198. #=================================================
  199. if [ $enable_dhcp -eq 1 ]
  200. then
  201. max_dhcp_range=250
  202. dhcp_range=100
  203. # Define the dhcp range from the current ip
  204. ip_beginning_part=$(echo "$localipv4" | cut -d. -f1-3)
  205. ip_fourth_part=$(echo "$localipv4" | cut -d. -f4)
  206. b_range=$(( $ip_fourth_part + $dhcp_range ))
  207. if [ $b_range -gt $max_dhcp_range ]; then
  208. b_range=$max_dhcp_range
  209. fi
  210. a_range=$(( $b_range - $dhcp_range ))
  211. # Get the gateway
  212. gateway=$(ip route | grep default | awk '{print $3;}')
  213. # And the mac adress
  214. hw_adress=$(ip link | grep -A1 "$main_iface" | tail -n1 | awk '{print $2;}')
  215. # Copy the config file
  216. cp "../conf/02-pihole-dhcp.conf" "/etc/dnsmasq.d/"
  217. # And set the config
  218. ynh_replace_string "__A_RANGE__" "$ip_beginning_part.$a_range" "/etc/dnsmasq.d/02-pihole-dhcp.conf"
  219. ynh_replace_string "__B_RANGE__" "$ip_beginning_part.$b_range" "/etc/dnsmasq.d/02-pihole-dhcp.conf"
  220. ynh_replace_string "__GATEWAY__" "$gateway" "/etc/dnsmasq.d/02-pihole-dhcp.conf"
  221. # Set a static ip for the server.
  222. echo "dhcp-host=$hw_adress,$localipv4" > "/etc/dnsmasq.d/04-pihole-static-dhcp.conf"
  223. fi
  224. # Open the UDP port 67 for dhcp
  225. ynh_exec_fully_quiet yunohost firewall allow UDP 67 --no-upnp
  226. #=================================================
  227. # RESTART DNSMASQ
  228. #=================================================
  229. ynh_systemd_action --action=restart --service_name=dnsmasq
  230. #=================================================
  231. # INSTALL THE CRON JOB
  232. #=================================================
  233. cp $pihole_local_repo/advanced/pihole.cron /etc/cron.d/pihole
  234. # Remove git usage for version. Which fails because we use here a release instead of master.
  235. ynh_replace_string ".*updatechecker.*" "#&" /etc/cron.d/pihole
  236. #=================================================
  237. # BUILD THE LISTS WITH GRAVITY
  238. #=================================================
  239. cp "$pihole_local_repo/adlists.default" "$pihole_storage/adlists.default"
  240. ynh_exec_warn_less /opt/pihole/gravity.sh
  241. #=================================================
  242. # START PIHOLE-FTL
  243. #=================================================
  244. ynh_systemd_action --action=restart --service_name=pihole-FTL
  245. #=================================================
  246. # SET UP THE CONF_REGEN HOOK
  247. #=================================================
  248. cp ../conf/dnsmasq_regenconf_hook /usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app
  249. #=================================================
  250. # GENERIC FINALISATION
  251. #=================================================
  252. # ENABLE SERVICE IN ADMIN PANEL
  253. #=================================================
  254. yunohost service add pihole-FTL --log "/var/log/pihole-FTL.log"
  255. #=================================================
  256. # RESTRAIN THE ACCESS TO THE ADMIN ONLY
  257. #=================================================
  258. yunohost app addaccess --users=$admin $app
  259. #=================================================
  260. # RELOAD NGINX
  261. #=================================================
  262. ynh_systemd_action --action=reload --service_name=nginx
  263. #=================================================
  264. # SEND A README FOR THE ADMIN
  265. #=================================================
  266. if [ $enable_dhcp -eq 1 ]
  267. then
  268. dhcp_alert="You asked to use the internal DHCP server of dnsmasq with PiHole.
  269. You should really read the documentation about that, https://github.com/YunoHost-Apps/pihole_ynh/blob/master/dhcp.md
  270. "
  271. else
  272. dhcp_alert=""
  273. fi
  274. message="${dhcp_alert}If you facing an issue or want to improve this app, please open a new issue in this project: https://github.com/YunoHost-Apps/pihole_ynh"
  275. ynh_send_readme_to_admin --app_message="$message" --recipients="$admin"