2016-10-22 17:43:16 +02:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
# Location of files which contains list of protected files
|
2018-10-24 10:00:20 +02:00
|
|
|
PROTECTED_FILE_LIST='/etc/qubes/protected-files.d'
|
2018-11-15 20:08:46 +01:00
|
|
|
PER_VM_PROTECTED_FILE_LIST='/rw/config/protected-files.d'
|
2016-10-22 17:43:16 +02:00
|
|
|
|
|
|
|
qsvc() {
|
|
|
|
# Returns whether a service is enabled.
|
|
|
|
# Usage: qsvc <nameofservice>
|
|
|
|
#
|
|
|
|
# Must only be used after qubes-sysinit has started.
|
|
|
|
# See qsvc_early for more information.
|
|
|
|
local count=100
|
|
|
|
while [ ! -e /var/run/qubes-service-environment ] ; do
|
|
|
|
if [ "$count" = "0" ] ; then
|
|
|
|
echo "qsvc: Warning: qubes-sysinit has not finished executing yet" >&2
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
sleep 0.1
|
|
|
|
count=$(( count - 1 ))
|
|
|
|
done
|
|
|
|
[ -e /var/run/qubes-service/"$1" ]
|
|
|
|
}
|
|
|
|
|
|
|
|
under_systemd() {
|
|
|
|
pidof systemd >/dev/null 2>&1
|
|
|
|
}
|
|
|
|
|
|
|
|
systemd_version_changed() {
|
|
|
|
under_systemd || return
|
2017-09-30 04:49:21 +02:00
|
|
|
systemd_pkg_version=$(systemctl --version|head -n 1)
|
2016-10-22 17:43:16 +02:00
|
|
|
if dmesg | grep -q "$systemd_pkg_version running in system mode."; then
|
2016-10-28 07:02:53 +02:00
|
|
|
return 1
|
2016-10-22 17:43:16 +02:00
|
|
|
fi
|
2016-10-28 07:02:53 +02:00
|
|
|
return 0
|
2016-10-22 17:43:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
have_qubesdb() {
|
|
|
|
# Tests whether qubesdb-read exists and can be executed.
|
|
|
|
type qubesdb-read >/dev/null 2>&1
|
|
|
|
}
|
|
|
|
|
|
|
|
have_qrexec_agent() {
|
|
|
|
# Tests whether qrexec-agent exists and can be executed.
|
|
|
|
PATH=/usr/lib/qubes type qrexec-agent >/dev/null 2>&1
|
|
|
|
}
|
|
|
|
|
|
|
|
qubes_vm_type() {
|
|
|
|
qubesdb-read /qubes-vm-type
|
|
|
|
}
|
|
|
|
|
|
|
|
is_netvm() {
|
|
|
|
[ "$(qubes_vm_type)" = "NetVM" ]
|
|
|
|
}
|
|
|
|
|
|
|
|
is_appvm() {
|
|
|
|
[ "$(qubes_vm_type)" = "AppVM" ]
|
|
|
|
}
|
|
|
|
|
|
|
|
is_proxyvm() {
|
|
|
|
[ "$(qubes_vm_type)" = "ProxyVM" ]
|
|
|
|
}
|
|
|
|
|
|
|
|
is_templatevm() {
|
|
|
|
[ "$(qubes_vm_type)" = "TemplateVM" ]
|
|
|
|
}
|
|
|
|
|
|
|
|
is_dispvm() {
|
2020-02-14 17:24:29 +01:00
|
|
|
[ "$(qubesdb-read /type)" = "DispVM" ]
|
2016-10-22 17:43:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
is_fully_persistent() {
|
|
|
|
[ "$(qubesdb-read /qubes-vm-persistence)" = "full" ]
|
|
|
|
}
|
|
|
|
|
|
|
|
is_rwonly_persistent() {
|
|
|
|
[ "$(qubesdb-read /qubes-vm-persistence)" = "rw-only" ]
|
|
|
|
}
|
|
|
|
|
|
|
|
is_updateable() {
|
|
|
|
[ "$(qubesdb-read /qubes-vm-updateable)" = "True" ]
|
|
|
|
}
|
|
|
|
|
|
|
|
reload_random_seed() {
|
2016-11-13 23:37:49 +01:00
|
|
|
local seed
|
|
|
|
seed=$(qubesdb-read /qubes-random-seed)
|
2016-11-17 09:30:49 +01:00
|
|
|
echo "$seed" | base64 -d > /dev/urandom
|
2016-10-22 17:43:16 +02:00
|
|
|
qubesdb-rm /qubes-random-seed
|
|
|
|
}
|
|
|
|
|
|
|
|
is_protected_file() {
|
2018-12-06 14:44:42 +01:00
|
|
|
local ret=1
|
2018-10-24 10:00:20 +02:00
|
|
|
local pfilelist
|
|
|
|
for pfilelist in "$PROTECTED_FILE_LIST" "$PER_VM_PROTECTED_FILE_LIST" ; do
|
|
|
|
if test -d "$pfilelist" ; then
|
|
|
|
# If this succeeds, we return immediately to the caller.
|
|
|
|
# If not, we let the loop continue.
|
|
|
|
grep -Fxrq --exclude='*.rpmsave' --exclude='*~' --exclude='*.rpmnew' --exclude='*.rpmold' -- "${1}" "$pfilelist" 2>/dev/null && return 0 || ret="$?"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
return "$ret"
|
2016-10-22 17:43:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
umount_retry() {
|
|
|
|
local count=5
|
|
|
|
while mountpoint -q "$1" ; do
|
|
|
|
if umount "$1" ; then break ; fi
|
|
|
|
echo "Something prevents unmounting $1:" >&2
|
|
|
|
fuser -vmM "$1" >&2
|
|
|
|
if [ "$count" = "0" ] ; then
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
sleep 5
|
|
|
|
count=$(( count - 1 ))
|
|
|
|
done
|
|
|
|
return 0
|
|
|
|
}
|
2016-11-13 22:20:46 +01:00
|
|
|
|
2019-05-18 12:19:54 +02:00
|
|
|
get_mac_from_iface() {
|
|
|
|
local iface="$1"
|
2020-12-04 03:23:18 +01:00
|
|
|
local mac=
|
2019-06-20 16:06:25 +02:00
|
|
|
if [ "x$iface" != "x" ] && [ -e "/sys/class/net/$iface" ]; then
|
2019-05-18 12:19:54 +02:00
|
|
|
mac="$(cat "/sys/class/net/$iface/address")"
|
|
|
|
fi
|
|
|
|
echo "$mac"
|
|
|
|
}
|
|
|
|
|
2019-05-12 23:29:48 +02:00
|
|
|
get_iface_from_mac() {
|
|
|
|
local mac="$1"
|
2020-12-04 03:23:18 +01:00
|
|
|
local iface=
|
2019-05-18 10:42:13 +02:00
|
|
|
if [ "x$mac" != "x" ]; then
|
|
|
|
iface="$(ip -o link | grep -i "$mac" | awk '{print $2}' | cut -d ':' -f1)"
|
|
|
|
fi
|
2019-05-12 23:29:48 +02:00
|
|
|
echo "$iface"
|
|
|
|
}
|
|
|
|
|
|
|
|
get_qubes_managed_iface() {
|
|
|
|
local mac
|
|
|
|
local qubes_iface
|
2019-06-20 16:06:25 +02:00
|
|
|
mac="$(qubesdb-read /qubes-mac 2> /dev/null)"
|
2020-12-18 05:24:28 +01:00
|
|
|
if [ -z "$mac" ]; then
|
|
|
|
# no qubes-managed network interface
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
# Load the module explicitly here, to avoid waiting for udev doing that
|
|
|
|
[ -e /sys/module/xen_netfront ] || modprobe xen-netfront || :
|
2019-05-12 23:29:48 +02:00
|
|
|
qubes_iface="$(get_iface_from_mac "$mac")"
|
2019-05-16 22:18:12 +02:00
|
|
|
if [ "x$qubes_iface" != "x" ]; then
|
|
|
|
echo "$qubes_iface"
|
2020-12-04 12:28:27 +01:00
|
|
|
elif [ -e /sys/class/net/eth0 ]; then
|
2019-05-16 22:18:12 +02:00
|
|
|
echo eth0
|
|
|
|
fi
|
2019-05-12 23:29:48 +02:00
|
|
|
}
|
|
|
|
|
2019-06-22 18:05:45 +02:00
|
|
|
# Based on: https://forums.gentoo.org/viewtopic-t-888736-start-0.html
|
2019-06-18 16:07:36 +02:00
|
|
|
get_prefix_from_subnet() {
|
|
|
|
local subnet="$1"
|
2019-06-22 18:05:45 +02:00
|
|
|
local x=${subnet##*255.}
|
|
|
|
set -- 0^^^128^192^224^240^248^252^254^ $(( (${#1} - ${#x})*2 )) "${x%%.*}"
|
|
|
|
x=${1%%$3*}
|
|
|
|
prefix=$(( $2 + (${#x}/4) ))
|
2019-06-18 16:07:36 +02:00
|
|
|
|
|
|
|
if [ "x$prefix" != "x" ]; then
|
|
|
|
echo "$prefix"
|
|
|
|
else
|
|
|
|
echo "32"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2016-11-13 22:20:46 +01:00
|
|
|
initialize_home() {
|
|
|
|
local home_root
|
|
|
|
local mode
|
2017-09-30 04:49:21 +02:00
|
|
|
#local user
|
2016-11-13 22:20:46 +01:00
|
|
|
local uid
|
|
|
|
local gid
|
|
|
|
local homedir
|
|
|
|
local homedirwithouthome
|
|
|
|
local pair
|
|
|
|
local homedir_uid
|
|
|
|
local homedir_gid
|
|
|
|
local waitpid
|
|
|
|
local waitpids
|
|
|
|
|
|
|
|
home_root="$1"
|
|
|
|
mode="$2"
|
|
|
|
|
|
|
|
if [ -z "$home_root" ] ; then
|
|
|
|
echo "initialize_home() needs a target home root directory, such as /rw/home, as first parameter" >&2
|
|
|
|
return 64
|
|
|
|
fi
|
|
|
|
|
2017-09-30 04:49:21 +02:00
|
|
|
if [ "$mode" != "unconditionally" ] && [ "$mode" != "ifneeded" ] ; then
|
2016-11-13 22:20:46 +01:00
|
|
|
echo "initialize_home() second parameter must be 'unconditionally' or 'ifneeded'" >&2
|
|
|
|
return 64
|
|
|
|
fi
|
|
|
|
|
|
|
|
if ! [ -d "$home_root" ] ; then
|
|
|
|
echo "initialize_home: populating $home_root" >&2
|
|
|
|
mkdir -p "$home_root"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Chown home if users' UIDs have changed - can be the case on template switch.
|
|
|
|
for pair in $(getent passwd | awk -F : '/\/home/ { print $1":"$3":"$4":"$6 } ') ; do
|
2017-09-30 04:49:21 +02:00
|
|
|
#user=$(echo "$pair" | awk -F : ' { print $1 } ')
|
2016-11-13 22:20:46 +01:00
|
|
|
uid=$(echo "$pair" | awk -F : ' { print $2 } ')
|
|
|
|
gid=$(echo "$pair" | awk -F : ' { print $3 } ')
|
|
|
|
homedir=$(echo "$pair" | awk -F : ' { print $4 } ')
|
2017-09-30 04:49:21 +02:00
|
|
|
homedirwithouthome=${homedir#/home/}
|
2016-11-13 22:20:46 +01:00
|
|
|
if ! test -d "$home_root/$homedirwithouthome" || [ "$mode" = "unconditionally" ] ; then
|
2018-04-13 00:35:08 +02:00
|
|
|
echo "initialize_home: populating $mode $home_root/$homedirwithouthome from /etc/skel" >&2
|
|
|
|
mkdir -p "$home_root/$homedirwithouthome"
|
|
|
|
cp -af -T /etc/skel "$home_root/$homedirwithouthome"
|
2016-11-13 22:20:46 +01:00
|
|
|
echo "initialize_home: adjusting permissions $mode on $home_root/$homedirwithouthome" >&2
|
|
|
|
chown -R "$uid" "$home_root/$homedirwithouthome" &
|
|
|
|
waitpids="$!"
|
|
|
|
chgrp -R "$gid" "$home_root/$homedirwithouthome" &
|
|
|
|
waitpids="$waitpids $!"
|
|
|
|
chmod 700 "$home_root/$homedirwithouthome" &
|
|
|
|
waitpids="$waitpids $!"
|
|
|
|
for waitpid in $waitpids ; do wait "$waitpid" ; done ; waitpids=
|
|
|
|
fi
|
|
|
|
waitpids=
|
2017-09-30 04:49:21 +02:00
|
|
|
homedir_uid=$(stat --format=%u "$home_root/$homedirwithouthome")
|
|
|
|
homedir_gid=$(stat --format=%g "$home_root/$homedirwithouthome")
|
2016-11-13 22:20:46 +01:00
|
|
|
if [ "$uid" -ne "$homedir_uid" ]; then
|
|
|
|
echo "initialize_home: adjusting ownership on $home_root/$homedirwithouthome to $uid" >&2
|
|
|
|
find "$home_root/$homedirwithouthome" -uid "$homedir_uid" -print0 | xargs -0 chown "$uid" &
|
|
|
|
waitpids="$waitpids $!"
|
|
|
|
fi
|
|
|
|
if [ "$gid" -ne "$homedir_gid" ]; then
|
|
|
|
echo "initialize_home: adjusting groupship on $home_root/$homedirwithouthome to $gid" >&2
|
|
|
|
find "$home_root/$homedirwithouthome" -gid "$homedir_gid" -print0 | xargs -0 chgrp "$gid" &
|
|
|
|
waitpids="$waitpids $!"
|
|
|
|
fi
|
|
|
|
for waitpid in $waitpids ; do wait "$waitpid" ; done ; waitpids=
|
|
|
|
done
|
2019-06-20 16:06:25 +02:00
|
|
|
}
|