install 12 KB

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