functions 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. local decoded
  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. grep -Fxrq --exclude='*.rpmsave' --exclude='*~' --exclude='*.rpmnew' --exclude='*.rpmold' -- "${1}" "$PROTECTED_FILE_LIST" 2>/dev/null
  87. }
  88. umount_retry() {
  89. local count=5
  90. while mountpoint -q "$1" ; do
  91. if umount "$1" ; then break ; fi
  92. echo "Something prevents unmounting $1:" >&2
  93. fuser -vmM "$1" >&2
  94. if [ "$count" = "0" ] ; then
  95. return 1
  96. fi
  97. sleep 5
  98. count=$(( count - 1 ))
  99. done
  100. return 0
  101. }
  102. initialize_home() {
  103. local home_root
  104. local mode
  105. local user
  106. local uid
  107. local gid
  108. local homedir
  109. local homedirwithouthome
  110. local pair
  111. local homedir_uid
  112. local homedir_gid
  113. local waitpid
  114. local waitpids
  115. home_root="$1"
  116. mode="$2"
  117. if [ -z "$home_root" ] ; then
  118. echo "initialize_home() needs a target home root directory, such as /rw/home, as first parameter" >&2
  119. return 64
  120. fi
  121. if [ "$mode" != "unconditionally" -a "$mode" != "ifneeded" ] ; then
  122. echo "initialize_home() second parameter must be 'unconditionally' or 'ifneeded'" >&2
  123. return 64
  124. fi
  125. if ! [ -d "$home_root" ] ; then
  126. echo "initialize_home: populating $home_root" >&2
  127. mkdir -p "$home_root"
  128. fi
  129. # Chown home if users' UIDs have changed - can be the case on template switch.
  130. for pair in $(getent passwd | awk -F : '/\/home/ { print $1":"$3":"$4":"$6 } ') ; do
  131. user=$(echo "$pair" | awk -F : ' { print $1 } ')
  132. uid=$(echo "$pair" | awk -F : ' { print $2 } ')
  133. gid=$(echo "$pair" | awk -F : ' { print $3 } ')
  134. homedir=$(echo "$pair" | awk -F : ' { print $4 } ')
  135. homedirwithouthome=$(echo "$homedir" | sed 's|^/home/||')
  136. if ! test -d "$home_root/$homedirwithouthome" || [ "$mode" = "unconditionally" ] ; then
  137. if [ "$homedir" == "/home/user" -a -d "/home.orig/$homedirwithouthome" ] ; then
  138. echo "initialize_home: populating $mode $home_root/$homedirwithouthome from /home.orig/$homedirwithouthome" >&2
  139. mkdir -p "$home_root/$homedirwithouthome"
  140. cp -af -T "/home.orig/$homedirwithouthome" "$home_root/$homedirwithouthome"
  141. else
  142. echo "initialize_home: populating $mode $home_root/$homedirwithouthome from /etc/skel" >&2
  143. mkdir -p "$home_root/$homedirwithouthome"
  144. cp -af -T /etc/skel "$home_root/$homedirwithouthome"
  145. fi
  146. echo "initialize_home: adjusting permissions $mode on $home_root/$homedirwithouthome" >&2
  147. chown -R "$uid" "$home_root/$homedirwithouthome" &
  148. waitpids="$!"
  149. chgrp -R "$gid" "$home_root/$homedirwithouthome" &
  150. waitpids="$waitpids $!"
  151. chmod 700 "$home_root/$homedirwithouthome" &
  152. waitpids="$waitpids $!"
  153. for waitpid in $waitpids ; do wait "$waitpid" ; done ; waitpids=
  154. fi
  155. waitpids=
  156. homedir_uid=$(ls -dn "$home_root/$homedirwithouthome" | awk '{print $3}')
  157. homedir_gid=$(ls -dn "$home_root/$homedirwithouthome" | awk '{print $4}')
  158. if [ "$uid" -ne "$homedir_uid" ]; then
  159. echo "initialize_home: adjusting ownership on $home_root/$homedirwithouthome to $uid" >&2
  160. find "$home_root/$homedirwithouthome" -uid "$homedir_uid" -print0 | xargs -0 chown "$uid" &
  161. waitpids="$waitpids $!"
  162. fi
  163. if [ "$gid" -ne "$homedir_gid" ]; then
  164. echo "initialize_home: adjusting groupship on $home_root/$homedirwithouthome to $gid" >&2
  165. find "$home_root/$homedirwithouthome" -gid "$homedir_gid" -print0 | xargs -0 chgrp "$gid" &
  166. waitpids="$waitpids $!"
  167. fi
  168. for waitpid in $waitpids ; do wait "$waitpid" ; done ; waitpids=
  169. done
  170. }