install 11 KB

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