functions 6.5 KB

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