update-proxy-configs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. # shellcheck source=init/functions
  25. . /usr/lib/qubes/init/functions
  26. BEGIN_MARKER="### QUBES BEGIN ###"
  27. END_MARKER="### QUBES END ###"
  28. set -e
  29. ### helper functions begin ###
  30. # set proxy in given config file
  31. update_conf() {
  32. local CONF_PATH="$1"
  33. local CONF_OPTIONS="$2"
  34. # Ensure that Qubes conf markers are present in the file
  35. if ! grep -q "$BEGIN_MARKER" "$CONF_PATH"; then
  36. if grep -q "$END_MARKER" "$CONF_PATH"; then
  37. echo "ERROR: found QUBES END marker but not QUBES BEGIN in ${CONF_PATH}" >&2
  38. echo "Fix the file by either removing both of them, or adding missing back and retry" >&2
  39. exit 1
  40. fi
  41. cp "$CONF_PATH" "${CONF_PATH}.qubes-orig"
  42. echo "$BEGIN_MARKER" >> "$CONF_PATH"
  43. echo "$END_MARKER" >> "$CONF_PATH"
  44. elif ! grep -q "$END_MARKER" "$CONF_PATH"; then
  45. echo "ERROR: found QUBES BEGIN marker but not QUBES END in ${CONF_PATH}" >&2
  46. echo "Fix the file by either removing both of them, or adding missing back and retry" >&2
  47. exit 1
  48. fi
  49. # Prepare config block
  50. local tmpfile
  51. tmpfile=$(mktemp)
  52. cat > "${tmpfile}" <<EOF
  53. # This part of configuration, until QUBES END, is automatically generated by
  54. # $0. All changes here will be overriden.
  55. # If you want to override any option set here, set it again to desired value,
  56. # below this section
  57. $CONF_OPTIONS
  58. EOF
  59. # And insert it between the markers
  60. sed -i -e "/^$BEGIN_MARKER$/,/^$END_MARKER$/{
  61. /^$END_MARKER$/b
  62. /^$BEGIN_MARKER$/!d
  63. r ${tmpfile}
  64. }" "${CONF_PATH}"
  65. rm -f "${tmpfile}"
  66. }
  67. ### helper functions end
  68. # Determine whether the proxy should be used
  69. if qsvc yum-proxy-setup || qsvc updates-proxy-setup ; then
  70. PROXY_ADDR_BASE="127.0.0.1:8082"
  71. PROXY_ADDR="http://${PROXY_ADDR_BASE}/"
  72. PROXY_CONF_ENTRY="proxy=$PROXY_ADDR"
  73. else
  74. PROXY_ADDR=""
  75. # do not proxy at all (for example dnf.conf doesn't tolerate empty entry)
  76. PROXY_CONF_ENTRY=""
  77. fi
  78. # For programs supporting .d style configs, it's simple
  79. if [ -d /etc/apt/apt.conf.d ]; then
  80. if [ -n "$PROXY_ADDR" ]; then
  81. cat > /etc/apt/apt.conf.d/01qubes-proxy <<EOF
  82. ### This file is automatically generated by Qubes ($0 script).
  83. ### All modifications here will be lost.
  84. ### If you want to override some of this settings, create another file under
  85. ### /etc/apt/apt.conf.d.
  86. # Use Qubes Update Proxy
  87. Acquire::http::Proxy "$PROXY_ADDR";
  88. Acquire::tor::proxy "$PROXY_ADDR";
  89. EOF
  90. else
  91. rm -f /etc/apt/apt.conf.d/01qubes-proxy
  92. fi
  93. fi
  94. # Yum at least supports including an individual config files
  95. if [ -d /etc/yum.conf.d ]; then
  96. cat > /etc/yum.conf.d/qubes-proxy.conf <<EOF
  97. ### This file is automatically generated by Qubes ($0 script).
  98. ### All modifications here will be lost.
  99. ### If you want to override some of this settings, add them in /etc/yum.conf
  100. ### below a "include=/etc/yum.conf.d/qubes-proxy.conf" line.
  101. $PROXY_CONF_ENTRY
  102. EOF
  103. fi
  104. # Pacman (archlinux) also
  105. if [ -d /etc/pacman.d ]; then
  106. if [ -n "$PROXY_ADDR" ]; then
  107. mkdir -p /run/qubes/bin
  108. cat > /run/qubes/bin/pacman <<EOF
  109. #!/bin/bash
  110. ### This file is automatically generated by Qubes ($0 script).
  111. ### All modifications here will be lost.
  112. exec env ALL_PROXY=$PROXY_ADDR /usr/bin/pacman "\$@"
  113. EOF
  114. chmod +x /run/qubes/bin/pacman
  115. cat > /etc/profile.d/qubes-proxy.sh << EOF
  116. ### This file is automatically generated by Qubes ($0 script).
  117. ### All modifications here will be lost.
  118. export PATH=/run/qubes/bin:\$PATH
  119. EOF
  120. else
  121. rm -f /run/qubes/bin/pacman
  122. rm -f /etc/profile.d/qubes-proxy.sh
  123. fi
  124. fi
  125. # DNF configuration doesn't support including other files
  126. # https://bugzilla.redhat.com/show_bug.cgi?id=1352234
  127. if [ -e /etc/dnf/dnf.conf ]; then
  128. update_conf /etc/dnf/dnf.conf "$PROXY_CONF_ENTRY"
  129. fi
  130. # The same goes for PackageKit...
  131. # https://bugs.freedesktop.org/show_bug.cgi?id=96788
  132. if [ -e /etc/PackageKit/PackageKit.conf ]; then
  133. update_conf /etc/PackageKit/PackageKit.conf "ProxyHTTP=$PROXY_ADDR"
  134. fi
  135. # Portage (Gentoo)
  136. if [ -e /etc/portage/make.conf ]; then
  137. update_conf /etc/portage/make.conf "http_proxy=\"$PROXY_ADDR\"
  138. https_proxy=\"$PROXY_ADDR\"
  139. RSYNC_PROXY=\"${PROXY_ADDR_BASE}\""
  140. # Current workaround for gpg not resolving key servers used behing proxy
  141. # See QubesOS/qubes-issues#6013
  142. if [ -n "$PROXY_ADDR" ]; then
  143. update_conf /etc/hosts "127.0.0.1 keys.gentoo.org keys.gnupg.net"
  144. else
  145. update_conf /etc/hosts ""
  146. fi
  147. fi