install 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. #!/bin/bash
  2. #=================================================
  3. # GENERIC STARTING
  4. #=================================================
  5. # IMPORT GENERIC HELPERS
  6. #=================================================
  7. source _common.sh
  8. source /usr/share/yunohost/helpers
  9. source _ynh_add_fpm_config
  10. # Load common variables for all scripts.
  11. source _variables
  12. #=================================================
  13. # MANAGE FAILURE OF THE SCRIPT
  14. #=================================================
  15. # Exit if an error occurs during the execution of the script
  16. ynh_abort_if_errors
  17. #=================================================
  18. # RETRIEVE ARGUMENTS FROM THE MANIFEST
  19. #=================================================
  20. domain=$YNH_APP_ARG_DOMAIN
  21. path_url=$YNH_APP_ARG_PATH
  22. admin=$YNH_APP_ARG_ADMIN
  23. query_logging=$YNH_APP_ARG_QUERY_LOGGING
  24. enable_dhcp=$YNH_APP_ARG_ENABLE_DHCP
  25. pihole_version="$YNH_APP_ARG_PIHOLE_VERSION"
  26. app=$YNH_APP_INSTANCE_NAME
  27. #=================================================
  28. # CHECK IF THE APP CAN BE INSTALLED WITH THIS ARGS
  29. #=================================================
  30. ynh_script_progression --message="Validating installation parameters..." --weight=2
  31. final_path=/var/www/$app
  32. test ! -e "$final_path" || ynh_die --message="This path already contains a folder"
  33. # Register (book) web path
  34. ynh_webpath_register --app=$app --domain=$domain --path_url=$path_url
  35. #=================================================
  36. # STORE SETTINGS FROM MANIFEST
  37. #=================================================
  38. ynh_script_progression --message="Storing installation settings..." --weight=3
  39. ynh_app_setting_set --app=$app --key=domain --value=$domain
  40. ynh_app_setting_set --app=$app --key=path --value=$path_url
  41. ynh_app_setting_set --app=$app --key=admin --value=$admin
  42. ynh_app_setting_set --app=$app --key=query_logging --value=$query_logging
  43. ynh_app_setting_set --app=$app --key=enable_dhcp --value=$enable_dhcp
  44. ynh_app_setting_set --app=$app --key=pihole_version --value="$pihole_version"
  45. ynh_app_setting_set --app=$app --key=overwrite_setupvars --value=1
  46. ynh_app_setting_set --app=$app --key=overwrite_ftl --value=1
  47. ynh_app_setting_set --app=$app --key=overwrite_nginx --value=1
  48. ynh_app_setting_set --app=$app --key=overwrite_phpfpm --value=1
  49. ynh_app_setting_set --app=$app --key=admin_mail_html --value=1
  50. #=================================================
  51. # STANDARD MODIFICATIONS
  52. #=================================================
  53. # FIND AND OPEN A PORT
  54. #=================================================
  55. ynh_script_progression --message="Configuring firewall..." --weight=12
  56. # Find a free port
  57. port=$(ynh_find_port --port=4711)
  58. if [ $port -gt 4720 ]
  59. then
  60. ynh_die --message="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."
  61. fi
  62. # Open this port
  63. ynh_exec_fully_quiet yunohost firewall allow --no-upnp TCP $port
  64. ynh_app_setting_set --app=$app --key=port --value=$port
  65. # Disable the port 53 for upnp
  66. ynh_exec_fully_quiet yunohost firewall disallow Both 53 --no-reload
  67. ynh_exec_fully_quiet yunohost firewall allow Both 53 --no-upnp
  68. #=================================================
  69. # INSTALL DEPENDENCIES
  70. #=================================================
  71. ynh_script_progression --message="Installing dependencies..." --weight=12
  72. ynh_install_app_dependencies $app_depencencies
  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. if [ "$pihole_version" == "Last 3.X" ]
  81. then
  82. # Install the version 3.3.1
  83. ynh_setup_source --dest_dir="$pihole_local_repo" --source_id=app_3
  84. # Install admin dashboard
  85. ynh_setup_source --dest_dir="$final_path" --source_id=admin_dashboard_3
  86. else
  87. # Install the last version available
  88. ynh_setup_source --dest_dir="$pihole_local_repo" --source_id=app_last
  89. # Install admin dashboard
  90. ynh_setup_source --dest_dir="$final_path" --source_id=admin_dashboard_last
  91. fi
  92. #=================================================
  93. # NGINX CONFIGURATION
  94. #=================================================
  95. ynh_script_progression --message="Configuring nginx web server..." --weight=2
  96. # Create a dedicated nginx config
  97. ynh_add_nginx_config
  98. #=================================================
  99. # CREATE DEDICATED USER
  100. #=================================================
  101. ynh_script_progression --message="Configuring system user..." --weight=2
  102. # Create a dedicated system user
  103. ynh_system_user_create --username=$app
  104. #=================================================
  105. # PHP-FPM CONFIGURATION
  106. #=================================================
  107. ynh_script_progression --message="Configuring php-fpm..." --weight=2
  108. # Create a dedicated php-fpm config
  109. ynh_add_fpm_config --usage=low --footprint=low
  110. #=================================================
  111. # SPECIFIC SETUP
  112. #=================================================
  113. # CREATE DIRECTORIES AND POPULATE THEM
  114. #=================================================
  115. ynh_script_progression --message="Creating and populating directories..."
  116. pihole_storage="/etc/pihole"
  117. mkdir -p "$pihole_storage"
  118. chown $app: -R "$pihole_storage"
  119. pihole_dir="/opt/pihole"
  120. mkdir -p "$pihole_dir"
  121. # Make a copy of Pi-Hole scripts
  122. cp -a "$pihole_local_repo/gravity.sh" "$pihole_dir/"
  123. cp -a $pihole_local_repo/advanced/Scripts/*.sh "$pihole_dir/"
  124. # And copy this fucking COL_TABLE file...
  125. cp -a "$pihole_local_repo/advanced/Scripts/COL_TABLE" "$pihole_dir/"
  126. #=================================================
  127. # COPY PI-HOLE MAIN SCRIPT
  128. #=================================================
  129. ynh_script_progression --message="Copying Pi-Hole main script..."
  130. cp -a "$pihole_local_repo/pihole" /usr/local/bin/
  131. cp -a "$pihole_local_repo/advanced/bash-completion/pihole" /etc/bash_completion.d/pihole
  132. #=================================================
  133. # CREATE LOG FILES
  134. #=================================================
  135. touch /var/log/pihole.log
  136. chmod 644 /var/log/pihole.log
  137. dnsmasq_user=$(grep DNSMASQ_USER= /etc/init.d/dnsmasq | cut -d'"' -f2)
  138. chown $dnsmasq_user:root /var/log/pihole.log
  139. #=================================================
  140. # CREATE SUDOER FILE
  141. #=================================================
  142. # This sudoers config allow pihole to execute /usr/local/bin/pihole as root without password. Nothing more.
  143. if [ "$pihole_version" == "Last 3.X" ]
  144. then
  145. cp "$pihole_local_repo/advanced/pihole.sudo" /etc/sudoers.d/pihole
  146. else
  147. cp "$pihole_local_repo/advanced/Templates/pihole.sudo" /etc/sudoers.d/pihole
  148. fi
  149. echo "$app ALL=NOPASSWD: /usr/local/bin/pihole" >> /etc/sudoers.d/pihole
  150. # echo "Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin" >> /etc/sudoers.d/pihole
  151. chmod 0440 /etc/sudoers.d/pihole
  152. #=================================================
  153. # INSTALL LOGROTATE SCRIPT FOR PI-HOLE
  154. #=================================================
  155. if [ "$pihole_version" == "Last 3.X" ]
  156. then
  157. cp "$pihole_local_repo/advanced/logrotate" "$pihole_storage/logrotate"
  158. else
  159. cp "$pihole_local_repo/advanced/Templates/logrotate" "$pihole_storage/logrotate"
  160. fi
  161. sed -i "/# su #/d;" "$pihole_storage/logrotate"
  162. #=================================================
  163. # INSTALLATION OF PIHOLE-FTL
  164. #=================================================
  165. ynh_script_progression --message="Installing PiHole-FTL..." --weight=30
  166. # Get the source of Pi-Hole-FTL
  167. FTL_temp_path=$(mktemp -d)
  168. if [ "$pihole_version" == "Last 3.X" ]
  169. then
  170. # Install the version 3.3.1
  171. ynh_setup_source --dest_dir="$FTL_temp_path" --source_id=FTL_3
  172. else
  173. # Install the last version available
  174. ynh_setup_source --dest_dir="$FTL_temp_path" --source_id=FTL_last
  175. fi
  176. # Instead of downloading a binary file, we're going to compile it
  177. ( cd "$FTL_temp_path"
  178. ynh_exec_warn_less make
  179. ynh_exec_warn_less make install )
  180. ynh_secure_remove --file="$FTL_temp_path"
  181. cp "../conf/pihole-FTL.conf" "$pihole_storage"
  182. # Calculate and store the config file checksum into the app settings
  183. ynh_store_file_checksum --file="$pihole_storage/pihole-FTL.conf"
  184. if [ "$pihole_version" == "Last 3.X" ]
  185. then
  186. # Version 3.3.1
  187. cp -a $pihole_local_repo/advanced/pihole-FTL.service /etc/init.d/pihole-FTL
  188. chmod +x /etc/init.d/pihole-FTL
  189. ynh_exec_warn_less systemctl enable pihole-FTL
  190. else
  191. # Last version available
  192. # Stopped dnsmasq to replace it by pihole-FTL
  193. ynh_systemd_action --action=stop --service_name=dnsmasq
  194. systemctl disable dnsmasq
  195. # rm /lib/systemd/system/dnsmasq.service
  196. mv /lib/systemd/system/dnsmasq.service /lib/systemd/system/.dnsmasq.service
  197. # rm /etc/init.d/dnsmasq
  198. mv /etc/init.d/dnsmasq /etc/init.d/.dnsmasq
  199. cp -a $pihole_local_repo/advanced/Templates/pihole-FTL.service /etc/init.d/pihole-FTL
  200. chmod +x /etc/init.d/pihole-FTL
  201. ynh_exec_warn_less systemctl enable pihole-FTL
  202. # Move dnsmasq to preserve the current version
  203. mv /usr/sbin/dnsmasq /usr/sbin/dnsmasq.backup_by_pihole
  204. # Replace dnsmasq by pihole-FTL
  205. # NOTE: pihole-FTL is actually a modified version of dnsmasq
  206. # https://github.com/pi-hole/FTL/tree/master/dnsmasq
  207. update-alternatives --install /usr/sbin/dnsmasq dnsmasq /usr/bin/pihole-FTL 50
  208. update-alternatives --install /usr/sbin/dnsmasq dnsmasq /usr/sbin/dnsmasq.backup_by_pihole 40
  209. # cp /etc/init.d/pihole-FTL /etc/init.d/dnsmasq
  210. # systemctl enable dnsmasq
  211. # ln --symbolic /run/pihole-FTL.pid /run/dnsmasq/dnsmasq.pid
  212. # lrwxrwxrwx 1 root root 35 Jun 22 2018 /etc/systemd/system/multi-user.target.wants/dnsmasq.service -> /lib/systemd/system/dnsmasq.service
  213. # /run/systemd/generator.late/pihole-FTL.service
  214. sudo ln -s /run/systemd/generator.late/pihole-FTL.service /etc/systemd/system/multi-user.target.wants/dnsmasq.service
  215. sudo systemctl daemon-reload
  216. # sudo yunohost app install github/pihole_ynh/ --args "admin=mcrudelis&pihole_version=Last available&" -f -n
  217. # >>> It does work, as we have both dnsmasq and pihole-FTL as the same.
  218. # But no more dns resolution...
  219. # sudo systemctl daemon-reload
  220. # /lib/systemd/system/dnsmasq.service
  221. # /etc/init.d/dnsmasq
  222. # systemctl stop dnsmasq
  223. # systemctl disable dnsmasq
  224. # If I remove /lib/systemd/system/dnsmasq.service and /etc/init.d/dnsmasq, it works.
  225. # sudo systemctl daemon-reload
  226. # But we don't have dnsmasq anymore...
  227. # Duplicating /etc/init.d/pihole-FTL in /etc/init.d/dnsmasq does work
  228. fi
  229. #=================================================
  230. # BUILD VARIABLES FILE
  231. #=================================================
  232. setupVars="$pihole_storage/setupVars.conf"
  233. # Get the default network interface
  234. main_iface=$(ip route | grep --max-count=1 default | awk '{print $5;}')
  235. echo "PIHOLE_INTERFACE=$main_iface" > $setupVars
  236. echo "IPV4_ADDRESS=127.0.0.1" >> $setupVars
  237. echo "IPV6_ADDRESS=::1" >> $setupVars
  238. echo "PIHOLE_DNS_1=" >> $setupVars
  239. echo "PIHOLE_DNS_2=" >> $setupVars
  240. if [ $query_logging -eq 1 ]; then
  241. query_logging=true
  242. else
  243. query_logging=false
  244. fi
  245. echo "QUERY_LOGGING=$query_logging" >> $setupVars
  246. echo "INSTALL_WEB=true" >> $setupVars
  247. # Calculate and store the config file checksum into the app settings
  248. ynh_store_file_checksum --file="$setupVars"
  249. #=================================================
  250. # SET UP DNSMASQ CONFIG
  251. #=================================================
  252. ynh_script_progression --message="Setting up Dnsmasq config..." --weight=2
  253. # ynh_systemd_action --action=stop --service_name=dnsmasq
  254. pihole_dnsmasq_config="/etc/dnsmasq.d/01-pihole.conf"
  255. cp "$pihole_local_repo/advanced/01-pihole.conf" $pihole_dnsmasq_config
  256. # Use dns from /etc/resolv.dnsmasq.conf
  257. ynh_replace_string --match_string="@DNS1@" --replace_string="" --target_file=$pihole_dnsmasq_config
  258. ynh_replace_string --match_string="@DNS2@" --replace_string="" --target_file=$pihole_dnsmasq_config
  259. ynh_replace_string --match_string="^no-resolv" --replace_string="#no-resolv" --target_file=$pihole_dnsmasq_config
  260. ynh_replace_string --match_string="@INT@" --replace_string="$main_iface" --target_file=$pihole_dnsmasq_config
  261. if [ "$query_logging" = "true" ]; then
  262. ynh_replace_string --match_string="^#log-queries" --replace_string="log-queries" --target_file=$pihole_dnsmasq_config
  263. else
  264. ynh_replace_string --match_string="^log-queries" --replace_string="#log-queries" --target_file=$pihole_dnsmasq_config
  265. fi
  266. # Fix a too recent option for our dnsmasq version.
  267. ynh_replace_string --match_string="log-queries=extra" --replace_string="log-queries" --target_file=$pihole_dnsmasq_config
  268. # Calculate and store the config file checksum into the app settings
  269. ynh_store_file_checksum --file="$pihole_dnsmasq_config"
  270. # To prevent any conflict with the original dnsmasq config, comment cache-size in the original config.
  271. ynh_replace_string --match_string="^cache-size=" --replace_string="#pihole# cache-size=" --target_file=/etc/dnsmasq.conf
  272. #=================================================
  273. # CONFIGURE DNS FOR THE LOCAL DOMAINS
  274. #=================================================
  275. ynh_script_progression --message="Configuring dns for the local domains..." --weight=7
  276. # Find the IP associated to the network interface
  277. localipv4=$(ip address | grep "${main_iface}\$" | awk '{print $2;}' | cut -d/ -f1)
  278. # List all YunoHost domains
  279. while read perdomain
  280. do
  281. # Comment domain resolution in /etc/hosts on 127.0.0.1, because they can interfere with the local network resolution.
  282. ynh_replace_string --match_string="^127.0.0.1.*$perdomain" --replace_string="#Commented by pihole# &" --target_file=/etc/hosts
  283. # And add a resolution on the local IP instead
  284. grep -q "^$localipv4.*$perdomain" /etc/hosts || \
  285. echo "$localipv4 $perdomain #Added by pihole#" >> /etc/hosts
  286. done <<< "$(yunohost domain list | grep "\." | sed 's/.*: \|.*- //')"
  287. #=================================================
  288. # ENABLE DHCP SERVER
  289. #=================================================
  290. if [ $enable_dhcp -eq 1 ]
  291. then
  292. ynh_script_progression --message="Enabling dhcp server..."
  293. max_dhcp_range=250
  294. dhcp_range=100
  295. # Define the dhcp range from the current ip
  296. ip_beginning_part=$(echo "$localipv4" | cut -d. -f1-3)
  297. ip_fourth_part=$(echo "$localipv4" | cut -d. -f4)
  298. b_range=$(( $ip_fourth_part + $dhcp_range ))
  299. if [ $b_range -gt $max_dhcp_range ]; then
  300. b_range=$max_dhcp_range
  301. fi
  302. a_range=$(( $b_range - $dhcp_range ))
  303. # Get the gateway
  304. gateway=$(ip route | grep default | awk '{print $3;}')
  305. # And the mac adress
  306. hw_adress=$(ip link | grep -A1 "$main_iface" | tail -n1 | awk '{print $2;}')
  307. # Copy the config file
  308. cp "../conf/02-pihole-dhcp.conf" "/etc/dnsmasq.d/"
  309. # And set the config
  310. ynh_replace_string --match_string="__A_RANGE__" --replace_string="$ip_beginning_part.$a_range" --target_file="/etc/dnsmasq.d/02-pihole-dhcp.conf"
  311. ynh_replace_string --match_string="__B_RANGE__" --replace_string="$ip_beginning_part.$b_range" --target_file="/etc/dnsmasq.d/02-pihole-dhcp.conf"
  312. ynh_replace_string --match_string="__GATEWAY__" --replace_string="$gateway" --target_file="/etc/dnsmasq.d/02-pihole-dhcp.conf"
  313. # Set a static ip for the server.
  314. echo "dhcp-host=$hw_adress,$localipv4" > "/etc/dnsmasq.d/04-pihole-static-dhcp.conf"
  315. fi
  316. # Open the UDP port 67 for dhcp
  317. ynh_exec_fully_quiet yunohost firewall allow UDP 67 --no-upnp
  318. #=================================================
  319. # INSTALL CRON JOB
  320. #=================================================
  321. if [ "$pihole_version" == "Last 3.X" ]
  322. then
  323. cp $pihole_local_repo/advanced/pihole.cron /etc/cron.d/pihole
  324. else
  325. cp $pihole_local_repo/advanced/Templates/pihole.cron /etc/cron.d/pihole
  326. fi
  327. # Remove git usage for version. Which fails because we use here a release instead of master.
  328. ynh_replace_string --match_string=".*updatechecker.*" --replace_string="#&" --target_file=/etc/cron.d/pihole
  329. #=================================================
  330. # RESTART DNSMASQ
  331. #=================================================
  332. ynh_script_progression --message="Restarting Dnsmasq..." --weight=2
  333. # ynh_systemd_action --action=restart --service_name=dnsmasq
  334. #=================================================
  335. # START PIHOLE-FTL
  336. #=================================================
  337. if [ "$pihole_version" == "Last 3.X" ]
  338. then
  339. ynh_script_progression --message="Restarting PiHole-FTL..." --weight=2
  340. ynh_systemd_action --action=restart --service_name=pihole-FTL
  341. fi
  342. sleep 5
  343. #=================================================
  344. # BUILD THE LISTS WITH GRAVITY
  345. #=================================================
  346. ynh_script_progression --message="Building the lists with Gravity..." --weight=7
  347. if [ "$pihole_version" == "Last 3.X" ]
  348. then
  349. cp "$pihole_local_repo/adlists.default" "$pihole_storage/adlists.default"
  350. else
  351. cp "../conf/adlists.default" "$pihole_storage/adlists.list"
  352. fi
  353. ynh_exec_warn_less /opt/pihole/gravity.sh
  354. #=================================================
  355. # SET UP CONF_REGEN HOOK
  356. #=================================================
  357. cp ../conf/dnsmasq_regenconf_hook /usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app
  358. #=================================================
  359. # GENERIC FINALISATION
  360. #=================================================
  361. # ADVERTISE SERVICE IN ADMIN PANEL
  362. #=================================================
  363. if [ "$pihole_version" == "Last 3.X" ]
  364. then
  365. yunohost service add pihole-FTL --description "PiHole backend service" --log "/var/log/pihole-FTL.log"
  366. fi
  367. #=================================================
  368. # RESTRAIN THE ACCESS TO THE ADMIN ONLY
  369. #=================================================
  370. ynh_script_progression --message="Restraining the access to the admin only..." --weight=2
  371. yunohost app addaccess --users=$admin $app
  372. #=================================================
  373. # RELOAD NGINX
  374. #=================================================
  375. ynh_script_progression --message="Reloading nginx web server..." --weight=3
  376. ynh_systemd_action --service_name=nginx --action=reload
  377. #=================================================
  378. # SEND A README FOR THE ADMIN
  379. #=================================================
  380. # Get main domain and buid the url of the admin panel of the app.
  381. admin_panel="https://$(grep portal_domain /etc/ssowat/conf.json | cut -d'"' -f4)/yunohost/admin/#/apps/$app"
  382. if [ $enable_dhcp -eq 1 ]
  383. then
  384. dhcp_alert="You asked to use the internal DHCP server of dnsmasq with PiHole.
  385. 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__
  386. "
  387. else
  388. dhcp_alert=""
  389. fi
  390. 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__.
  391. 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__.
  392. 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
  393. ynh_send_readme_to_admin --app_message="mail_to_send" --recipients="$admin" --type=install
  394. #=================================================
  395. # END OF SCRIPT
  396. #=================================================
  397. ynh_script_progression --message="Installation of $app completed" --last