functions 5.9 KB

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