functions 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. get_prefix_from_subnet() {
  139. local subnet="$1"
  140. prefix="$(ipcalc -p 0.0.0.0 "$subnet" | sed 's/PREFIX=//')"
  141. if [ "x$prefix" != "x" ]; then
  142. echo "$prefix"
  143. else
  144. echo "32"
  145. fi
  146. }
  147. initialize_home() {
  148. local home_root
  149. local mode
  150. #local user
  151. local uid
  152. local gid
  153. local homedir
  154. local homedirwithouthome
  155. local pair
  156. local homedir_uid
  157. local homedir_gid
  158. local waitpid
  159. local waitpids
  160. home_root="$1"
  161. mode="$2"
  162. if [ -z "$home_root" ] ; then
  163. echo "initialize_home() needs a target home root directory, such as /rw/home, as first parameter" >&2
  164. return 64
  165. fi
  166. if [ "$mode" != "unconditionally" ] && [ "$mode" != "ifneeded" ] ; then
  167. echo "initialize_home() second parameter must be 'unconditionally' or 'ifneeded'" >&2
  168. return 64
  169. fi
  170. if ! [ -d "$home_root" ] ; then
  171. echo "initialize_home: populating $home_root" >&2
  172. mkdir -p "$home_root"
  173. fi
  174. # Chown home if users' UIDs have changed - can be the case on template switch.
  175. for pair in $(getent passwd | awk -F : '/\/home/ { print $1":"$3":"$4":"$6 } ') ; do
  176. #user=$(echo "$pair" | awk -F : ' { print $1 } ')
  177. uid=$(echo "$pair" | awk -F : ' { print $2 } ')
  178. gid=$(echo "$pair" | awk -F : ' { print $3 } ')
  179. homedir=$(echo "$pair" | awk -F : ' { print $4 } ')
  180. homedirwithouthome=${homedir#/home/}
  181. if ! test -d "$home_root/$homedirwithouthome" || [ "$mode" = "unconditionally" ] ; then
  182. echo "initialize_home: populating $mode $home_root/$homedirwithouthome from /etc/skel" >&2
  183. mkdir -p "$home_root/$homedirwithouthome"
  184. cp -af -T /etc/skel "$home_root/$homedirwithouthome"
  185. echo "initialize_home: adjusting permissions $mode on $home_root/$homedirwithouthome" >&2
  186. chown -R "$uid" "$home_root/$homedirwithouthome" &
  187. waitpids="$!"
  188. chgrp -R "$gid" "$home_root/$homedirwithouthome" &
  189. waitpids="$waitpids $!"
  190. chmod 700 "$home_root/$homedirwithouthome" &
  191. waitpids="$waitpids $!"
  192. for waitpid in $waitpids ; do wait "$waitpid" ; done ; waitpids=
  193. fi
  194. waitpids=
  195. homedir_uid=$(stat --format=%u "$home_root/$homedirwithouthome")
  196. homedir_gid=$(stat --format=%g "$home_root/$homedirwithouthome")
  197. if [ "$uid" -ne "$homedir_uid" ]; then
  198. echo "initialize_home: adjusting ownership on $home_root/$homedirwithouthome to $uid" >&2
  199. find "$home_root/$homedirwithouthome" -uid "$homedir_uid" -print0 | xargs -0 chown "$uid" &
  200. waitpids="$waitpids $!"
  201. fi
  202. if [ "$gid" -ne "$homedir_gid" ]; then
  203. echo "initialize_home: adjusting groupship on $home_root/$homedirwithouthome to $gid" >&2
  204. find "$home_root/$homedirwithouthome" -gid "$homedir_gid" -print0 | xargs -0 chgrp "$gid" &
  205. waitpids="$waitpids $!"
  206. fi
  207. for waitpid in $waitpids ; do wait "$waitpid" ; done ; waitpids=
  208. done
  209. }