functions 6.0 KB

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