functions 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. initialize_home() {
  112. local home_root
  113. local mode
  114. #local user
  115. local uid
  116. local gid
  117. local homedir
  118. local homedirwithouthome
  119. local pair
  120. local homedir_uid
  121. local homedir_gid
  122. local waitpid
  123. local waitpids
  124. home_root="$1"
  125. mode="$2"
  126. if [ -z "$home_root" ] ; then
  127. echo "initialize_home() needs a target home root directory, such as /rw/home, as first parameter" >&2
  128. return 64
  129. fi
  130. if [ "$mode" != "unconditionally" ] && [ "$mode" != "ifneeded" ] ; then
  131. echo "initialize_home() second parameter must be 'unconditionally' or 'ifneeded'" >&2
  132. return 64
  133. fi
  134. if ! [ -d "$home_root" ] ; then
  135. echo "initialize_home: populating $home_root" >&2
  136. mkdir -p "$home_root"
  137. fi
  138. # Chown home if users' UIDs have changed - can be the case on template switch.
  139. for pair in $(getent passwd | awk -F : '/\/home/ { print $1":"$3":"$4":"$6 } ') ; do
  140. #user=$(echo "$pair" | awk -F : ' { print $1 } ')
  141. uid=$(echo "$pair" | awk -F : ' { print $2 } ')
  142. gid=$(echo "$pair" | awk -F : ' { print $3 } ')
  143. homedir=$(echo "$pair" | awk -F : ' { print $4 } ')
  144. homedirwithouthome=${homedir#/home/}
  145. if ! test -d "$home_root/$homedirwithouthome" || [ "$mode" = "unconditionally" ] ; then
  146. echo "initialize_home: populating $mode $home_root/$homedirwithouthome from /etc/skel" >&2
  147. mkdir -p "$home_root/$homedirwithouthome"
  148. cp -af -T /etc/skel "$home_root/$homedirwithouthome"
  149. echo "initialize_home: adjusting permissions $mode on $home_root/$homedirwithouthome" >&2
  150. chown -R "$uid" "$home_root/$homedirwithouthome" &
  151. waitpids="$!"
  152. chgrp -R "$gid" "$home_root/$homedirwithouthome" &
  153. waitpids="$waitpids $!"
  154. chmod 700 "$home_root/$homedirwithouthome" &
  155. waitpids="$waitpids $!"
  156. for waitpid in $waitpids ; do wait "$waitpid" ; done ; waitpids=
  157. fi
  158. waitpids=
  159. homedir_uid=$(stat --format=%u "$home_root/$homedirwithouthome")
  160. homedir_gid=$(stat --format=%g "$home_root/$homedirwithouthome")
  161. if [ "$uid" -ne "$homedir_uid" ]; then
  162. echo "initialize_home: adjusting ownership on $home_root/$homedirwithouthome to $uid" >&2
  163. find "$home_root/$homedirwithouthome" -uid "$homedir_uid" -print0 | xargs -0 chown "$uid" &
  164. waitpids="$waitpids $!"
  165. fi
  166. if [ "$gid" -ne "$homedir_gid" ]; then
  167. echo "initialize_home: adjusting groupship on $home_root/$homedirwithouthome to $gid" >&2
  168. find "$home_root/$homedirwithouthome" -gid "$homedir_gid" -print0 | xargs -0 chgrp "$gid" &
  169. waitpids="$waitpids $!"
  170. fi
  171. for waitpid in $waitpids ; do wait "$waitpid" ; done ; waitpids=
  172. done
  173. }