functions 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. have_qubesdb() {
  34. # Tests whether qubesdb-read exists and can be executed.
  35. type qubesdb-read >/dev/null 2>&1
  36. }
  37. have_qrexec_agent() {
  38. # Tests whether qrexec-agent exists and can be executed.
  39. PATH=/usr/lib/qubes type qrexec-agent >/dev/null 2>&1
  40. }
  41. qubes_vm_type() {
  42. qubesdb-read /qubes-vm-type
  43. }
  44. is_netvm() {
  45. [ "$(qubes_vm_type)" = "NetVM" ]
  46. }
  47. is_appvm() {
  48. [ "$(qubes_vm_type)" = "AppVM" ]
  49. }
  50. is_proxyvm() {
  51. [ "$(qubes_vm_type)" = "ProxyVM" ]
  52. }
  53. is_templatevm() {
  54. [ "$(qubes_vm_type)" = "TemplateVM" ]
  55. }
  56. is_dispvm() {
  57. [ "$(qubes_vm_type)" = "DisposableVM" ]
  58. }
  59. is_fully_persistent() {
  60. [ "$(qubesdb-read /qubes-vm-persistence)" = "full" ]
  61. }
  62. is_rwonly_persistent() {
  63. [ "$(qubesdb-read /qubes-vm-persistence)" = "rw-only" ]
  64. }
  65. is_updateable() {
  66. [ "$(qubesdb-read /qubes-vm-updateable)" = "True" ]
  67. }
  68. reload_random_seed() {
  69. local seed
  70. seed=$(qubesdb-read /qubes-random-seed)
  71. echo "$seed" | base64 -d > /dev/urandom
  72. qubesdb-rm /qubes-random-seed
  73. }
  74. is_protected_file() {
  75. local ret=1
  76. local pfilelist
  77. for pfilelist in "$PROTECTED_FILE_LIST" "$PER_VM_PROTECTED_FILE_LIST" ; do
  78. if test -d "$pfilelist" ; then
  79. # If this succeeds, we return immediately to the caller.
  80. # If not, we let the loop continue.
  81. grep -Fxrq --exclude='*.rpmsave' --exclude='*~' --exclude='*.rpmnew' --exclude='*.rpmold' -- "${1}" "$pfilelist" 2>/dev/null && return 0 || ret="$?"
  82. fi
  83. done
  84. return "$ret"
  85. }
  86. umount_retry() {
  87. local count=5
  88. while mountpoint -q "$1" ; do
  89. if umount "$1" ; then break ; fi
  90. echo "Something prevents unmounting $1:" >&2
  91. fuser -vmM "$1" >&2
  92. if [ "$count" = "0" ] ; then
  93. return 1
  94. fi
  95. sleep 5
  96. count=$(( count - 1 ))
  97. done
  98. return 0
  99. }
  100. get_mac_from_iface() {
  101. local iface="$1"
  102. local mac
  103. if [ "x$iface" != "x" ] && [ -e "/sys/class/net/$iface" ]; then
  104. mac="$(cat "/sys/class/net/$iface/address")"
  105. fi
  106. echo "$mac"
  107. }
  108. get_iface_from_mac() {
  109. local mac="$1"
  110. local iface
  111. if [ "x$mac" != "x" ]; then
  112. iface="$(ip -o link | grep -i "$mac" | awk '{print $2}' | cut -d ':' -f1)"
  113. fi
  114. echo "$iface"
  115. }
  116. get_qubes_managed_iface() {
  117. local mac
  118. local qubes_iface
  119. mac="$(qubesdb-read /qubes-mac 2> /dev/null)"
  120. qubes_iface="$(get_iface_from_mac "$mac")"
  121. if [ "x$qubes_iface" != "x" ]; then
  122. echo "$qubes_iface"
  123. else
  124. echo eth0
  125. fi
  126. }
  127. # Based on: https://forums.gentoo.org/viewtopic-t-888736-start-0.html
  128. get_prefix_from_subnet() {
  129. local subnet="$1"
  130. local x=${subnet##*255.}
  131. set -- 0^^^128^192^224^240^248^252^254^ $(( (${#1} - ${#x})*2 )) "${x%%.*}"
  132. x=${1%%$3*}
  133. prefix=$(( $2 + (${#x}/4) ))
  134. if [ "x$prefix" != "x" ]; then
  135. echo "$prefix"
  136. else
  137. echo "32"
  138. fi
  139. }
  140. initialize_home() {
  141. local home_root
  142. local mode
  143. #local user
  144. local uid
  145. local gid
  146. local homedir
  147. local homedirwithouthome
  148. local pair
  149. local homedir_uid
  150. local homedir_gid
  151. local waitpid
  152. local waitpids
  153. home_root="$1"
  154. mode="$2"
  155. if [ -z "$home_root" ] ; then
  156. echo "initialize_home() needs a target home root directory, such as /rw/home, as first parameter" >&2
  157. return 64
  158. fi
  159. if [ "$mode" != "unconditionally" ] && [ "$mode" != "ifneeded" ] ; then
  160. echo "initialize_home() second parameter must be 'unconditionally' or 'ifneeded'" >&2
  161. return 64
  162. fi
  163. if ! [ -d "$home_root" ] ; then
  164. echo "initialize_home: populating $home_root" >&2
  165. mkdir -p "$home_root"
  166. fi
  167. # Chown home if users' UIDs have changed - can be the case on template switch.
  168. for pair in $(getent passwd | awk -F : '/\/home/ { print $1":"$3":"$4":"$6 } ') ; do
  169. #user=$(echo "$pair" | awk -F : ' { print $1 } ')
  170. uid=$(echo "$pair" | awk -F : ' { print $2 } ')
  171. gid=$(echo "$pair" | awk -F : ' { print $3 } ')
  172. homedir=$(echo "$pair" | awk -F : ' { print $4 } ')
  173. homedirwithouthome=${homedir#/home/}
  174. if ! test -d "$home_root/$homedirwithouthome" || [ "$mode" = "unconditionally" ] ; then
  175. echo "initialize_home: populating $mode $home_root/$homedirwithouthome from /etc/skel" >&2
  176. mkdir -p "$home_root/$homedirwithouthome"
  177. cp -af -T /etc/skel "$home_root/$homedirwithouthome"
  178. echo "initialize_home: adjusting permissions $mode on $home_root/$homedirwithouthome" >&2
  179. chown -R "$uid" "$home_root/$homedirwithouthome" &
  180. waitpids="$!"
  181. chgrp -R "$gid" "$home_root/$homedirwithouthome" &
  182. waitpids="$waitpids $!"
  183. chmod 700 "$home_root/$homedirwithouthome" &
  184. waitpids="$waitpids $!"
  185. for waitpid in $waitpids ; do wait "$waitpid" ; done ; waitpids=
  186. fi
  187. waitpids=
  188. homedir_uid=$(stat --format=%u "$home_root/$homedirwithouthome")
  189. homedir_gid=$(stat --format=%g "$home_root/$homedirwithouthome")
  190. if [ "$uid" -ne "$homedir_uid" ]; then
  191. echo "initialize_home: adjusting ownership on $home_root/$homedirwithouthome to $uid" >&2
  192. find "$home_root/$homedirwithouthome" -uid "$homedir_uid" -print0 | xargs -0 chown "$uid" &
  193. waitpids="$waitpids $!"
  194. fi
  195. if [ "$gid" -ne "$homedir_gid" ]; then
  196. echo "initialize_home: adjusting groupship on $home_root/$homedirwithouthome to $gid" >&2
  197. find "$home_root/$homedirwithouthome" -gid "$homedir_gid" -print0 | xargs -0 chgrp "$gid" &
  198. waitpids="$waitpids $!"
  199. fi
  200. for waitpid in $waitpids ; do wait "$waitpid" ; done ; waitpids=
  201. done
  202. }