functions 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #!/bin/bash
  2. # Location of files which contains list of protected files
  3. PROTECTED_FILE_LIST='/etc/qubes/protected-files.d'
  4. PER_VM_PROTECTED_FILE_LIST='/rw/config/protected-files.d'
  5. qsvc() {
  6. # Returns whether a service is enabled.
  7. # Usage: qsvc <nameofservice>
  8. #
  9. # Must only be used after qubes-sysinit has started.
  10. # See qsvc_early for more information.
  11. local count=100
  12. while [ ! -e /var/run/qubes-service-environment ] ; do
  13. if [ "$count" = "0" ] ; then
  14. echo "qsvc: Warning: qubes-sysinit has not finished executing yet" >&2
  15. break
  16. fi
  17. sleep 0.1
  18. count=$(( count - 1 ))
  19. done
  20. [ -e /var/run/qubes-service/"$1" ]
  21. }
  22. under_systemd() {
  23. pidof systemd >/dev/null 2>&1
  24. }
  25. systemd_version_changed() {
  26. under_systemd || return
  27. systemd_pkg_version=$(systemctl --version|head -n 1)
  28. if dmesg | grep -q "$systemd_pkg_version running in system mode."; then
  29. return 1
  30. fi
  31. return 0
  32. }
  33. possibly_run_save_script() {
  34. ENCODED_SCRIPT=$(qubesdb-read /qubes-save-script)
  35. if [ -z "$ENCODED_SCRIPT" ] ; then return ; fi
  36. tmpfile=$(mktemp /tmp/qubes-save-script.XXXXXXXXX)
  37. echo "$ENCODED_SCRIPT"|base64 -d >"$tmpfile"
  38. chmod 755 "$tmpfile"
  39. DISPLAY=:0 su - user -c "$tmpfile"
  40. ret=$?
  41. rm -f "$tmpfile"
  42. return $ret
  43. }
  44. have_qubesdb() {
  45. # Tests whether qubesdb-read exists and can be executed.
  46. type qubesdb-read >/dev/null 2>&1
  47. }
  48. have_qrexec_agent() {
  49. # Tests whether qrexec-agent exists and can be executed.
  50. PATH=/usr/lib/qubes type qrexec-agent >/dev/null 2>&1
  51. }
  52. qubes_vm_type() {
  53. qubesdb-read /qubes-vm-type
  54. }
  55. is_netvm() {
  56. [ "$(qubes_vm_type)" = "NetVM" ]
  57. }
  58. is_appvm() {
  59. [ "$(qubes_vm_type)" = "AppVM" ]
  60. }
  61. is_proxyvm() {
  62. [ "$(qubes_vm_type)" = "ProxyVM" ]
  63. }
  64. is_templatevm() {
  65. [ "$(qubes_vm_type)" = "TemplateVM" ]
  66. }
  67. is_dispvm() {
  68. [ "$(qubes_vm_type)" = "DisposableVM" ]
  69. }
  70. is_fully_persistent() {
  71. [ "$(qubesdb-read /qubes-vm-persistence)" = "full" ]
  72. }
  73. is_rwonly_persistent() {
  74. [ "$(qubesdb-read /qubes-vm-persistence)" = "rw-only" ]
  75. }
  76. is_updateable() {
  77. [ "$(qubesdb-read /qubes-vm-updateable)" = "True" ]
  78. }
  79. reload_random_seed() {
  80. local seed
  81. seed=$(qubesdb-read /qubes-random-seed)
  82. echo "$seed" | base64 -d > /dev/urandom
  83. qubesdb-rm /qubes-random-seed
  84. }
  85. is_protected_file() {
  86. local ret=1
  87. local pfilelist
  88. for pfilelist in "$PROTECTED_FILE_LIST" "$PER_VM_PROTECTED_FILE_LIST" ; do
  89. if test -d "$pfilelist" ; then
  90. # If this succeeds, we return immediately to the caller.
  91. # If not, we let the loop continue.
  92. grep -Fxrq --exclude='*.rpmsave' --exclude='*~' --exclude='*.rpmnew' --exclude='*.rpmold' -- "${1}" "$pfilelist" 2>/dev/null && return 0 || ret="$?"
  93. fi
  94. done
  95. return "$ret"
  96. }
  97. umount_retry() {
  98. local count=5
  99. while mountpoint -q "$1" ; do
  100. if umount "$1" ; then break ; fi
  101. echo "Something prevents unmounting $1:" >&2
  102. fuser -vmM "$1" >&2
  103. if [ "$count" = "0" ] ; then
  104. return 1
  105. fi
  106. sleep 5
  107. count=$(( count - 1 ))
  108. done
  109. return 0
  110. }
  111. get_mac_from_iface() {
  112. local iface="$1"
  113. local mac
  114. if [ "x$iface" != "x" ] && [ -e "/sys/class/net/$iface" ]; then
  115. mac="$(cat "/sys/class/net/$iface/address")"
  116. fi
  117. echo "$mac"
  118. }
  119. get_iface_from_mac() {
  120. local mac="$1"
  121. local iface
  122. if [ "x$mac" != "x" ]; then
  123. iface="$(ip -o link | grep -i "$mac" | awk '{print $2}' | cut -d ':' -f1)"
  124. fi
  125. echo "$iface"
  126. }
  127. get_qubes_managed_iface() {
  128. local mac
  129. local qubes_iface
  130. mac="$(qubesdb-read /qubes-mac 2> /dev/null)"
  131. qubes_iface="$(get_iface_from_mac "$mac")"
  132. if [ "x$qubes_iface" != "x" ]; then
  133. echo "$qubes_iface"
  134. else
  135. echo eth0
  136. fi
  137. }
  138. # Based on: https://forums.gentoo.org/viewtopic-t-888736-start-0.html
  139. get_prefix_from_subnet() {
  140. local subnet="$1"
  141. local x=${subnet##*255.}
  142. set -- 0^^^128^192^224^240^248^252^254^ $(( (${#1} - ${#x})*2 )) "${x%%.*}"
  143. x=${1%%$3*}
  144. prefix=$(( $2 + (${#x}/4) ))
  145. if [ "x$prefix" != "x" ]; then
  146. echo "$prefix"
  147. else
  148. echo "32"
  149. fi
  150. }
  151. initialize_home() {
  152. local home_root
  153. local mode
  154. #local user
  155. local uid
  156. local gid
  157. local homedir
  158. local homedirwithouthome
  159. local pair
  160. local homedir_uid
  161. local homedir_gid
  162. local waitpid
  163. local waitpids
  164. home_root="$1"
  165. mode="$2"
  166. if [ -z "$home_root" ] ; then
  167. echo "initialize_home() needs a target home root directory, such as /rw/home, as first parameter" >&2
  168. return 64
  169. fi
  170. if [ "$mode" != "unconditionally" ] && [ "$mode" != "ifneeded" ] ; then
  171. echo "initialize_home() second parameter must be 'unconditionally' or 'ifneeded'" >&2
  172. return 64
  173. fi
  174. if ! [ -d "$home_root" ] ; then
  175. echo "initialize_home: populating $home_root" >&2
  176. mkdir -p "$home_root"
  177. fi
  178. # Chown home if users' UIDs have changed - can be the case on template switch.
  179. for pair in $(getent passwd | awk -F : '/\/home/ { print $1":"$3":"$4":"$6 } ') ; do
  180. #user=$(echo "$pair" | awk -F : ' { print $1 } ')
  181. uid=$(echo "$pair" | awk -F : ' { print $2 } ')
  182. gid=$(echo "$pair" | awk -F : ' { print $3 } ')
  183. homedir=$(echo "$pair" | awk -F : ' { print $4 } ')
  184. homedirwithouthome=${homedir#/home/}
  185. if ! test -d "$home_root/$homedirwithouthome" || [ "$mode" = "unconditionally" ] ; then
  186. echo "initialize_home: populating $mode $home_root/$homedirwithouthome from /etc/skel" >&2
  187. mkdir -p "$home_root/$homedirwithouthome"
  188. cp -af -T /etc/skel "$home_root/$homedirwithouthome"
  189. echo "initialize_home: adjusting permissions $mode on $home_root/$homedirwithouthome" >&2
  190. chown -R "$uid" "$home_root/$homedirwithouthome" &
  191. waitpids="$!"
  192. chgrp -R "$gid" "$home_root/$homedirwithouthome" &
  193. waitpids="$waitpids $!"
  194. chmod 700 "$home_root/$homedirwithouthome" &
  195. waitpids="$waitpids $!"
  196. for waitpid in $waitpids ; do wait "$waitpid" ; done ; waitpids=
  197. fi
  198. waitpids=
  199. homedir_uid=$(stat --format=%u "$home_root/$homedirwithouthome")
  200. homedir_gid=$(stat --format=%g "$home_root/$homedirwithouthome")
  201. if [ "$uid" -ne "$homedir_uid" ]; then
  202. echo "initialize_home: adjusting ownership on $home_root/$homedirwithouthome to $uid" >&2
  203. find "$home_root/$homedirwithouthome" -uid "$homedir_uid" -print0 | xargs -0 chown "$uid" &
  204. waitpids="$waitpids $!"
  205. fi
  206. if [ "$gid" -ne "$homedir_gid" ]; then
  207. echo "initialize_home: adjusting groupship on $home_root/$homedirwithouthome to $gid" >&2
  208. find "$home_root/$homedirwithouthome" -gid "$homedir_gid" -print0 | xargs -0 chgrp "$gid" &
  209. waitpids="$waitpids $!"
  210. fi
  211. for waitpid in $waitpids ; do wait "$waitpid" ; done ; waitpids=
  212. done
  213. }