update-proxy-configs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #!/bin/bash
  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. # Source Qubes library.
  24. . /usr/lib/qubes/init/functions
  25. BEGIN_MARKER="### QUBES BEGIN ###"
  26. END_MARKER="### QUBES END ###"
  27. set -e
  28. ### helper functions begin ###
  29. # set proxy in given config file
  30. update_conf() {
  31. local CONF_PATH="$1"
  32. local CONF_OPTIONS="$2"
  33. # Ensure that Qubes conf markers are present in the file
  34. if ! grep -q "$BEGIN_MARKER" $CONF_PATH; then
  35. if grep -q "$END_MARKER" $CONF_PATH; then
  36. echo "ERROR: found QUBES END marker but not QUBES BEGIN in ${CONF_PATH}" >&2
  37. echo "Fix the file by either removing both of them, or adding missing back and retry" >&2
  38. exit 1
  39. fi
  40. cp $CONF_PATH ${CONF_PATH}.qubes-orig
  41. echo "$BEGIN_MARKER" >> $CONF_PATH
  42. echo "$END_MARKER" >> $CONF_PATH
  43. elif ! grep -q "$END_MARKER" $CONF_PATH; then
  44. echo "ERROR: found QUBES BEGIN marker but not QUBES END in ${CONF_PATH}" >&2
  45. echo "Fix the file by either removing both of them, or adding missing back and retry" >&2
  46. exit 1
  47. fi
  48. # Prepare config block
  49. local tmpfile=`mktemp`
  50. cat > ${tmpfile} <<EOF
  51. # This part of configuration, until QUBES END, is automatically generated by
  52. # $0. All changes here will be overriden.
  53. # If you want to override any option set here, set it again to desired value,
  54. # below this section
  55. $CONF_OPTIONS
  56. EOF
  57. # And insert it between the markers
  58. sed -i -e "/^$BEGIN_MARKER$/,/^$END_MARKER$/{
  59. /^$END_MARKER$/b
  60. /^$BEGIN_MARKER$/!d
  61. r ${tmpfile}
  62. }" ${CONF_PATH}
  63. rm -f ${tmpfile}
  64. }
  65. ### helper functions end
  66. # Determine whether the proxy should be used
  67. if qsvc yum-proxy-setup || qsvc updates-proxy-setup ; then
  68. PROXY_ADDR="http://10.137.255.254:8082/"
  69. PROXY_CONF_ENTRY="proxy=$PROXY_ADDR"
  70. else
  71. PROXY_ADDR=""
  72. # do not proxy at all (for example dnf.conf doesn't tolerate empty entry)
  73. PROXY_CONF_ENTRY=""
  74. fi
  75. # For programs supporting .d style configs, it's simple
  76. if [ -d /etc/apt/apt.conf.d ]; then
  77. if [ -n "$PROXY_ADDR" ]; then
  78. cat > /etc/apt/apt.conf.d/01qubes-proxy <<EOF
  79. ### This file is automatically generated by Qubes ($0 script).
  80. ### All modifications here will be lost.
  81. ### If you want to override some of this settings, create another file under
  82. ### /etc/apt/apt.conf.d.
  83. Acquire::http::Proxy "$PROXY_ADDR";
  84. EOF
  85. else
  86. rm -f /etc/apt/apt.conf.d/01qubes-proxy
  87. fi
  88. fi
  89. # Yum at least supports including an individual config files
  90. if [ -d /etc/yum.conf.d ]; then
  91. cat > /etc/yum.conf.d/qubes-proxy.conf <<EOF
  92. ### This file is automatically generated by Qubes ($0 script).
  93. ### All modifications here will be lost.
  94. ### If you want to override some of this settings, add them in /etc/yum.conf
  95. ### below a "include=/etc/yum.conf.d/qubes-proxy.conf" line.
  96. $PROXY_CONF_ENTRY
  97. EOF
  98. fi
  99. # Pacman (archlinux) also
  100. if [ -d /etc/pacman.d ]; then
  101. if [ -n "$PROXY_ADDR" ]; then
  102. cat > /etc/pacman.d/01-qubes-proxy.conf <<EOF
  103. ### This file is automatically generated by Qubes ($0 script).
  104. ### All modifications here will be lost.
  105. ### If you want to override some of this settings, create another file under
  106. ### /etc/pacman.d
  107. XferCommand = http_proxy=$PROXY_ADDR /usr/bin/curl -C - -f %u > %o
  108. EOF
  109. else
  110. rm -r /etc/pacman.d/01-qubes-proxy.conf
  111. fi
  112. fi
  113. # DNF configuration doesn't support including other files
  114. # https://bugzilla.redhat.com/show_bug.cgi?id=1352234
  115. if [ -e /etc/dnf/dnf.conf ]; then
  116. update_conf /etc/dnf/dnf.conf "$PROXY_CONF_ENTRY"
  117. fi
  118. # The same goes for PackageKit...
  119. # https://bugs.freedesktop.org/show_bug.cgi?id=96788
  120. if [ -e /etc/PackageKit/PackageKit.conf ]; then
  121. update_conf /etc/PackageKit/PackageKit.conf "ProxyHTTP=$PROXY_ADDR"
  122. fi