update-proxy-configs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/bin/sh
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2015 Marek Marczykowski-Górecki
  6. # <marmarek@invisiblethingslab.com>
  7. #
  8. # This program is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU General Public License
  10. # as published by the Free Software Foundation; either version 2
  11. # of the License, or (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program; if not, write to the Free Software
  20. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21. #
  22. #
  23. BEGIN_MARKER="### QUBES BEGIN ###"
  24. END_MARKER="### QUBES END ###"
  25. set -e
  26. ### helper functions begin ###
  27. # set proxy in given config file
  28. update_conf() {
  29. local CONF_PATH="$1"
  30. local CONF_OPTIONS="$2"
  31. # Ensure that Qubes conf markers are present in the file
  32. if ! grep -q "$BEGIN_MARKER" $CONF_PATH; then
  33. if grep -q "$END_MARKER" $CONF_PATH; then
  34. echo "ERROR: found QUBES END marker but not QUBES BEGIN in ${CONF_PATH}" >&2
  35. echo "Fix the file by either removing both of them, or adding missing back and retry" >&2
  36. exit 1
  37. fi
  38. cp $CONF_PATH ${CONF_PATH}.qubes-orig
  39. echo "$BEGIN_MARKER" >> $CONF_PATH
  40. echo "$END_MARKER" >> $CONF_PATH
  41. elif ! grep -q "$END_MARKER" $CONF_PATH; then
  42. echo "ERROR: found QUBES BEGIN marker but not QUBES END in ${CONF_PATH}" >&2
  43. echo "Fix the file by either removing both of them, or adding missing back and retry" >&2
  44. exit 1
  45. fi
  46. # Prepare config block
  47. cat > ${CONF_PATH}.qubes <<EOF
  48. # This part of configuration, until QUBES END, is automatically generated by
  49. # $0. All changes here will be overriden.
  50. # If you want to override any option set here, set it again to desired value,
  51. # below this section
  52. $CONF_OPTIONS
  53. EOF
  54. # And insert it between the markers
  55. sed -i -e "/^$BEGIN_MARKER$/,/^$END_MARKER$/{
  56. /^$END_MARKER$/b
  57. /^$BEGIN_MARKER$/!d
  58. r ${CONF_PATH}.qubes
  59. }" ${CONF_PATH}
  60. }
  61. ### helper functions end
  62. # Determine whether the proxy should be used
  63. if [ -f /var/run/qubes-service/yum-proxy-setup -o -f /var/run/qubes-service/updates-proxy-setup ]; then
  64. PROXY_ADDR="http://10.137.255.254:8082/"
  65. PROXY_CONF_ENTRY="proxy=$PROXY_ADDR"
  66. else
  67. PROXY_ADDR=""
  68. # do not proxy at all (for example dnf.conf doesn't tolerate empty entry)
  69. PROXY_CONF_ENTRY=""
  70. fi
  71. # For programs supporting .d style configs, it's simple
  72. if [ -d /etc/apt/apt.conf.d ]; then
  73. if [ -n "$PROXY_ADDR" ]; then
  74. cat > /etc/apt/apt.conf.d/01qubes-proxy <<EOF
  75. ### This file is automatically generated by Qubes ($0 script).
  76. ### All modifications here will be lost.
  77. ### If you want to override some of this settings, create another file under
  78. ### /etc/apt/apt.conf.d.
  79. Acquire::http::Proxy "$PROXY_ADDR";
  80. EOF
  81. else
  82. rm -f /etc/apt/apt.conf.d/01qubes-proxy
  83. fi
  84. fi
  85. # Yum at least supports including an individual config files
  86. if [ -d /etc/yum.conf.d ]; then
  87. cat > /etc/yum.conf.d/qubes-proxy.conf <<EOF
  88. ### This file is automatically generated by Qubes ($0 script).
  89. ### All modifications here will be lost.
  90. ### If you want to override some of this settings, add them in /etc/yum.conf
  91. ### below a "include=/etc/yum.conf.d/qubes-proxy.conf" line.
  92. $PROXY_CONF_ENTRY
  93. EOF
  94. fi
  95. # DNF configuration doesn't support including other files
  96. if [ -e /etc/dnf/dnf.conf ]; then
  97. update_conf /etc/dnf/dnf.conf "$PROXY_CONF_ENTRY"
  98. fi
  99. # The same goes for PackageKit...
  100. if [ -e /etc/PackageKit/PackageKit.conf ]; then
  101. update_conf /etc/PackageKit/PackageKit.conf "ProxyHTTP=$PROXY_ADDR"
  102. fi