#!/bin/bash

# Location of files which contains list of protected files
PROTECTED_FILE_LIST='/etc/qubes/protected-files.d'

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
    systemd_pkg_version=$(systemctl --version|head -n 1)
    if dmesg | grep -q "$systemd_pkg_version running in system mode."; then
        return 1
    fi
    return 0
}

possibly_run_save_script() {
    ENCODED_SCRIPT=$(qubesdb-read /qubes-save-script)
    if [ -z "$ENCODED_SCRIPT" ] ; then return ; fi
    tmpfile=$(mktemp /tmp/qubes-save-script.XXXXXXXXX)
    echo "$ENCODED_SCRIPT"|base64 -d >"$tmpfile"
    chmod 755 "$tmpfile"
    DISPLAY=:0 su - user -c "$tmpfile"
    ret=$?
    rm -f "$tmpfile"
    return $ret
}

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() {
    [ "$(qubes_vm_type)" = "DisposableVM" ]
}

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() {
    local seed
    seed=$(qubesdb-read /qubes-random-seed)
    echo "$seed" | base64 -d > /dev/urandom
    qubesdb-rm /qubes-random-seed
}

is_protected_file() {
    grep -Fxrq --exclude='*.rpmsave' --exclude='*~' --exclude='*.rpmnew' --exclude='*.rpmold' -- "${1}" "$PROTECTED_FILE_LIST" 2>/dev/null
}

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
}

initialize_home() {
    local home_root
    local mode
    #local user
    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

    if [ "$mode" != "unconditionally" ] && [ "$mode" != "ifneeded" ] ; then
        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
        #user=$(echo "$pair" | awk -F : ' { print $1 } ')
        uid=$(echo "$pair" | awk -F : ' { print $2 } ')
        gid=$(echo "$pair" | awk -F : ' { print $3 } ')
        homedir=$(echo "$pair" | awk -F : ' { print $4 } ')
        homedirwithouthome=${homedir#/home/}
        if ! test -d "$home_root/$homedirwithouthome" || [ "$mode" = "unconditionally" ] ; then
            if [ "$homedir" == "/home/user" ] && [ -d "/home.orig/$homedirwithouthome" ] ; then
                echo "initialize_home: populating $mode $home_root/$homedirwithouthome from /home.orig/$homedirwithouthome" >&2
                mkdir -p "$home_root/$homedirwithouthome"
                cp -af -T "/home.orig/$homedirwithouthome" "$home_root/$homedirwithouthome"
            else
                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"
            fi
            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=
        homedir_uid=$(stat --format=%u "$home_root/$homedirwithouthome")
        homedir_gid=$(stat --format=%g "$home_root/$homedirwithouthome")
        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
}