functions 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. have_qubesdb() {
  34. # Tests whether qubesdb-read exists and can be executed.
  35. type qubesdb-read >/dev/null 2>&1
  36. }
  37. have_qrexec_agent() {
  38. # Tests whether qrexec-agent exists and can be executed.
  39. PATH=/usr/lib/qubes type qrexec-agent >/dev/null 2>&1
  40. }
  41. qubes_vm_type() {
  42. qubesdb-read /qubes-vm-type
  43. }
  44. is_netvm() {
  45. [ "$(qubes_vm_type)" = "NetVM" ]
  46. }
  47. is_appvm() {
  48. [ "$(qubes_vm_type)" = "AppVM" ]
  49. }
  50. is_proxyvm() {
  51. [ "$(qubes_vm_type)" = "ProxyVM" ]
  52. }
  53. is_templatevm() {
  54. [ "$(qubes_vm_type)" = "TemplateVM" ]
  55. }
  56. is_dispvm() {
  57. [ "$(qubesdb-read /type)" = "DispVM" ]
  58. }
  59. is_fully_persistent() {
  60. [ "$(qubesdb-read /qubes-vm-persistence)" = "full" ]
  61. }
  62. is_rwonly_persistent() {
  63. [ "$(qubesdb-read /qubes-vm-persistence)" = "rw-only" ]
  64. }
  65. is_updateable() {
  66. [ "$(qubesdb-read /qubes-vm-updateable)" = "True" ]
  67. }
  68. reload_random_seed() {
  69. local seed
  70. seed=$(qubesdb-read /qubes-random-seed)
  71. echo "$seed" | base64 -d > /dev/urandom
  72. qubesdb-rm /qubes-random-seed
  73. }
  74. is_protected_file() {
  75. local ret=1
  76. local pfilelist
  77. for pfilelist in "$PROTECTED_FILE_LIST" "$PER_VM_PROTECTED_FILE_LIST" ; do
  78. if test -d "$pfilelist" ; then
  79. # If this succeeds, we return immediately to the caller.
  80. # If not, we let the loop continue.
  81. grep -Fxrq --exclude='*.rpmsave' --exclude='*~' --exclude='*.rpmnew' --exclude='*.rpmold' -- "${1}" "$pfilelist" 2>/dev/null && return 0 || ret="$?"
  82. fi
  83. done
  84. return "$ret"
  85. }
  86. umount_retry() {
  87. local count=5
  88. while mountpoint -q "$1" ; do
  89. if umount "$1" ; then break ; fi
  90. echo "Something prevents unmounting $1:" >&2
  91. fuser -vmM "$1" >&2
  92. if [ "$count" = "0" ] ; then
  93. return 1
  94. fi
  95. sleep 5
  96. count=$(( count - 1 ))
  97. done
  98. return 0
  99. }
  100. get_mac_from_iface() {
  101. local iface="$1"
  102. local mac=
  103. if [ "x$iface" != "x" ] && [ -e "/sys/class/net/$iface" ]; then
  104. mac="$(cat "/sys/class/net/$iface/address")"
  105. fi
  106. echo "$mac"
  107. }
  108. get_iface_from_mac() {
  109. local mac="$1"
  110. local iface=
  111. if [ "x$mac" != "x" ]; then
  112. iface="$(ip -o link | grep -i "$mac" | awk '{print $2}' | cut -d ':' -f1)"
  113. fi
  114. echo "$iface"
  115. }
  116. get_qubes_managed_iface() {
  117. local mac
  118. local qubes_iface
  119. mac="$(qubesdb-read /qubes-mac 2> /dev/null)"
  120. if [ -z "$mac" ]; then
  121. # no qubes-managed network interface
  122. return
  123. fi
  124. # Load the module explicitly here, to avoid waiting for udev doing that
  125. [ -e /sys/module/xen_netfront ] || modprobe xen-netfront || :
  126. qubes_iface="$(get_iface_from_mac "$mac")"
  127. if [ "x$qubes_iface" != "x" ]; then
  128. echo "$qubes_iface"
  129. elif [ -e /sys/class/net/eth0 ]; then
  130. echo eth0
  131. fi
  132. }
  133. # Based on: https://forums.gentoo.org/viewtopic-t-888736-start-0.html
  134. get_prefix_from_subnet() {
  135. local subnet="$1"
  136. local x=${subnet##*255.}
  137. set -- 0^^^128^192^224^240^248^252^254^ $(( (${#1} - ${#x})*2 )) "${x%%.*}"
  138. x=${1%%$3*}
  139. prefix=$(( $2 + (${#x}/4) ))
  140. if [ "x$prefix" != "x" ]; then
  141. echo "$prefix"
  142. else
  143. echo "32"
  144. fi
  145. }
  146. initialize_home() {
  147. local home_root
  148. local mode
  149. #local user
  150. local uid
  151. local gid
  152. local homedir
  153. local homedirwithouthome
  154. local pair
  155. local homedir_uid
  156. local homedir_gid
  157. local waitpid
  158. local waitpids
  159. home_root="$1"
  160. mode="$2"
  161. if [ -z "$home_root" ] ; then
  162. echo "initialize_home() needs a target home root directory, such as /rw/home, as first parameter" >&2
  163. return 64
  164. fi
  165. if [ "$mode" != "unconditionally" ] && [ "$mode" != "ifneeded" ] ; then
  166. echo "initialize_home() second parameter must be 'unconditionally' or 'ifneeded'" >&2
  167. return 64
  168. fi
  169. if ! [ -d "$home_root" ] ; then
  170. echo "initialize_home: populating $home_root" >&2
  171. mkdir -p "$home_root"
  172. fi
  173. # Chown home if users' UIDs have changed - can be the case on template switch.
  174. for pair in $(getent passwd | awk -F : '/\/home/ { print $1":"$3":"$4":"$6 } ') ; do
  175. #user=$(echo "$pair" | awk -F : ' { print $1 } ')
  176. uid=$(echo "$pair" | awk -F : ' { print $2 } ')
  177. gid=$(echo "$pair" | awk -F : ' { print $3 } ')
  178. homedir=$(echo "$pair" | awk -F : ' { print $4 } ')
  179. homedirwithouthome=${homedir#/home/}
  180. if ! test -d "$home_root/$homedirwithouthome" || [ "$mode" = "unconditionally" ] ; then
  181. echo "initialize_home: populating $mode $home_root/$homedirwithouthome from /etc/skel" >&2
  182. mkdir -p "$home_root/$homedirwithouthome"
  183. cp -af -T /etc/skel "$home_root/$homedirwithouthome"
  184. echo "initialize_home: adjusting permissions $mode on $home_root/$homedirwithouthome" >&2
  185. chown -R "$uid" "$home_root/$homedirwithouthome" &
  186. waitpids="$!"
  187. chgrp -R "$gid" "$home_root/$homedirwithouthome" &
  188. waitpids="$waitpids $!"
  189. chmod 700 "$home_root/$homedirwithouthome" &
  190. waitpids="$waitpids $!"
  191. for waitpid in $waitpids ; do wait "$waitpid" ; done ; waitpids=
  192. fi
  193. waitpids=
  194. homedir_uid=$(stat --format=%u "$home_root/$homedirwithouthome")
  195. homedir_gid=$(stat --format=%g "$home_root/$homedirwithouthome")
  196. if [ "$uid" -ne "$homedir_uid" ]; then
  197. echo "initialize_home: adjusting ownership on $home_root/$homedirwithouthome to $uid" >&2
  198. find "$home_root/$homedirwithouthome" -uid "$homedir_uid" -print0 | xargs -0 chown "$uid" &
  199. waitpids="$waitpids $!"
  200. fi
  201. if [ "$gid" -ne "$homedir_gid" ]; then
  202. echo "initialize_home: adjusting groupship on $home_root/$homedirwithouthome to $gid" >&2
  203. find "$home_root/$homedirwithouthome" -gid "$homedir_gid" -print0 | xargs -0 chgrp "$gid" &
  204. waitpids="$waitpids $!"
  205. fi
  206. for waitpid in $waitpids ; do wait "$waitpid" ; done ; waitpids=
  207. done
  208. }