HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

test/units/TEST-46-HOMED.sh

56,914 bytesGit mode 100755Download original fileLGPL-2.1-or-later

Git blob: a7cc3139d1b0f2df2639b77d3c46e8f4d4f641da

Lines 1001–1367 of 1,367

    test "$(run0 --property=SetCredential=pam.authtok.systemd-run0:quux -u subareatest --area=srub bash -c 'echo $HOME')" = "/home/srub"
    test "$(run0 --property=SetCredential=pam.authtok.systemd-run0:quux -u subareatest --area=srub bash -c 'echo $XDG_AREA')" = "srub"
    test "$(run0 --property=SetCredential=pam.authtok.systemd-run0:quux -u subareatest --area=srub bash -c 'echo $XDG_RUNTIME_DIR')" = "/run/user/$(id -u subareatest)/Areas/srub"
 
    # Verify that login into an area not owned by target user will be redirected to main area
    test "$(run0 --property=SetCredential=pam.authtok.systemd-run0:quux -u subareatest --area=root bash -c 'echo $HOME')" = "/home/subareatest"
    test "$(run0 --property=SetCredential=pam.authtok.systemd-run0:quux -u subareatest --area=root bash -c 'echo x$XDG_AREA')" = "x"
    test "$(run0 --property=SetCredential=pam.authtok.systemd-run0:quux -u subareatest --area=root bash -c 'echo $XDG_RUNTIME_DIR')" = "/run/user/$(id -u subareatest)"
 
    systemctl stop user@"$(id -u subareatest)".service
 
    wait_for_state subareatest inactive
    homectl remove subareatest
}
 
testcase_sign() {
    # Test signing key logic
    homectl list-signing-keys | grep local.public >/dev/null
    (! (homectl list-signing-keys | grep signtest.public >/dev/null))
 
    if built_with_musl; then
        # FIXME: musl does not support yescrypt. Use SHA512 and update signature.
        return 0
    fi
 
    print_identity() {
        cat <<\EOF
{
    "userName" : "signtest",
    "storage" : "directory",
    "disposition" : "regular",
    "privileged" : {
        "hashedPassword" : [
            "$y$j9T$I5Wxfm.fyg.RRWlgWw.rI1$gnQqGtbpPexqxZJkWMq8FxQi5Swc.CWeKtM8LwvEUB6"
        ]
    },
    "enforcePasswordPolicy" : false,
    "lastChangeUSec" : 1740677608017608,
    "lastPasswordChangeUSec" : 1740677608017608,
    "signature" : [
        {
            "data" : "Gl4wtc0sMjVnsH6FQwG/0M+x0nLI5cvvdtSSCttUu1gNtXqYn0UI4wZi/7zX35ERht6XHWDlP4d6V8HiAst4Dg==",
            "key" : "-----BEGIN PUBLIC KEY-----\nMCowBQYDK2VwAyEA6uvVaP1vh7O6nIbiOcvyIHRl4ihYSs0R7ctxtz2Zu7E=\n-----END PUBLIC KEY-----\n"
        }
    ],
    "secret" : {
        "password" : [
            "test"
        ]
    }
}
EOF
    }
 
    # Try with stripping the foreign signature first, this should just work
    print_identity | homectl create -P --identity=- --seize=yes
    wait_for_state signtest inactive
    homectl remove signtest
 
    # No try again, and don't strip the signature. It will be refused.
    (! (print_identity | homectl create -P --identity=- --seize=no))
 
    print_public_key() {
        cat <<EOF
-----BEGIN PUBLIC KEY-----
MCowBQYDK2VwAyEA6uvVaP1vh7O6nIbiOcvyIHRl4ihYSs0R7ctxtz2Zu7E=
-----END PUBLIC KEY-----
EOF
    }
 
    # Let's now add the signing key
    print_public_key | homectl add-signing-key --key-name=signtest.public
    homectl get-signing-key signtest.public | cmp - <(print_public_key)
    homectl list-signing-keys | grep local.public >/dev/null
    homectl list-signing-keys | grep signtest.public >/dev/null
 
    # Now create the account with this, it should work now
    print_identity | homectl create -P --identity=- --seize=no
 
    # Verify we can log in
    PASSWORD="test" homectl with signtest true
 
    # Remove the key, and check again ,should fail now
    wait_for_state signtest inactive
    homectl remove-signing-key signtest.public
    wait_for_state signtest inactive
    (! PASSWORD="test" homectl with signtest true)
 
    # Verify key is really gone
    homectl list-signing-keys | grep local.public >/dev/null
    (! (homectl list-signing-keys | grep signtest.public >/dev/null))
 
    # Test unregister + adopt
    mkdir /home/elsewhere
    mv /home/signtest.homedir /home/elsewhere/
    wait_for_state signtest absent
    homectl unregister signtest
    print_public_key | homectl add-signing-key --key-name=signtest.public
    homectl adopt /home/elsewhere/signtest.homedir
    PASSWORD="test" homectl with signtest true
 
    # Test register
    wait_for_state signtest inactive
    homectl unregister signtest
    homectl register /home/elsewhere/signtest.homedir/.identity
    wait_for_state signtest absent
    homectl unregister signtest
 
    # Test automatic fixation for anything in /home/
    mv /home/elsewhere/signtest.homedir /home
    rmdir /home/elsewhere
    wait_for_exist signtest
    PASSWORD="test" homectl with signtest true
 
    # add signing key via credential
    wait_for_state signtest inactive
    homectl remove-signing-key signtest.public
    (! (homectl list-signing-keys | grep signtest.public >/dev/null))
    systemd-run --wait -p "SetCredential=home.add-signing-key.signtest.public:$(print_public_key)" homectl firstboot
    homectl list-signing-keys | grep signtest.public >/dev/null
 
    # register user via credential
    mkdir /home/elsewhere2
    mv /home/signtest.homedir /home/elsewhere2/
    wait_for_state signtest absent
    homectl unregister signtest
    systemd-run --wait -p "LoadCredential=home.register.signtest:/home/elsewhere2/signtest.homedir/.identity" homectl firstboot
    homectl inspect signtest
    wait_for_state signtest absent
    homectl unregister signtest
    mv /home/elsewhere2/signtest.homedir /home/
    rmdir /home/elsewhere2
 
    # Remove it all again
    wait_for_exist signtest
    homectl remove-signing-key signtest.public
    homectl remove signtest
}
 
testcase_match() {
    # Test positive and negative matching
    NEWPASSWORD=test homectl create --storage=directory --nice=5 -P matchtest
    homectl inspect matchtest
    homectl inspect matchtest | grep "Nice: 5"
    PASSWORD=test homectl update -N --nice=7 -T --nice=3 matchtest
    homectl inspect matchtest
    homectl inspect matchtest | grep "Nice: 3"
    PASSWORD=test homectl update -A --default-area=quux1 matchtest
    homectl inspect matchtest
    homectl inspect matchtest | grep "Area: quux1"
    PASSWORD=test homectl update -N --default-area=quux2 matchtest
    homectl inspect matchtest
    homectl inspect matchtest | grep "Area: quux1"
    PASSWORD=test homectl update -T --default-area=quux3 matchtest
    homectl inspect matchtest
    homectl inspect matchtest | grep "Area: quux3"
    homectl remove matchtest
}
 
testcase_fscrypt() {
    if ! command -v mkfs.ext4 >/dev/null; then
        echo "e2fsprogs not installed, skipping fscrypt test."
        return 0
    fi
 
    local IMAGE MNT
    IMAGE="$(mktemp /tmp/fscrypt.XXXXXX.img)"
    MNT="$(mktemp -d /tmp/fscrypt-mnt.XXXXXX)"
    # shellcheck disable=SC2064
    trap "homectl deactivate fscrypttest 2>/dev/null || true; homectl remove fscrypttest 2>/dev/null || true; umount '$MNT' 2>/dev/null || true; rm -rf '$MNT' '$IMAGE'" RETURN ERR
 
    truncate -s 64M "$IMAGE"
    if ! mkfs.ext4 -q -O encrypt "$IMAGE"; then
        echo "mkfs.ext4 -O encrypt unsupported, skipping fscrypt test."
        return 0
    fi
 
    if ! mount -o loop "$IMAGE" "$MNT"; then
        echo "Cannot loop-mount fscrypt-capable ext4, skipping fscrypt test."
        return 0
    fi
 
    if ! NEWPASSWORD=fsfsfs1234 homectl create fscrypttest \
            --storage=fscrypt \
            --image-path="$MNT/fscrypttest" \
            --rate-limit-interval=1s --rate-limit-burst=1000; then
        echo "homed fscrypt backend not usable on this kernel, skipping."
        return 0
    fi
 
    inspect fscrypttest
 
    (! PASSWORD=wrongpass timeout 10s homectl authenticate fscrypttest </dev/null)
 
    PASSWORD=fsfsfs1234 homectl authenticate fscrypttest
    PASSWORD=fsfsfs1234 homectl activate fscrypttest
    inspect fscrypttest
 
    fscrypt_run0() {
        run0 --property=SetCredential=pam.authtok.systemd-run0:"$1" -u fscrypttest \
            bash -c 'keyctl link @u @s; eval "$1"' -- "$2"
    }
 
    fscrypt_run0 fsfsfs1234 'echo "hello fscrypt" >/home/fscrypttest/file1'
    [[ "$(fscrypt_run0 fsfsfs1234 'cat /home/fscrypttest/file1')" == "hello fscrypt" ]]
    fscrypt_run0 fsfsfs1234 'mkdir /home/fscrypttest/subdir'
    fscrypt_run0 fsfsfs1234 'dd if=/dev/urandom of=/home/fscrypttest/subdir/blob bs=4096 count=8 status=none'
    fscrypt_run0 fsfsfs1234 'cp /home/fscrypttest/subdir/blob /home/fscrypttest/subdir/blob.copy && cmp /home/fscrypttest/subdir/blob /home/fscrypttest/subdir/blob.copy'
    fscrypt_run0 fsfsfs1234 'echo appended >>/home/fscrypttest/file1 && grep -F appended /home/fscrypttest/file1 >/dev/null'
    fscrypt_run0 fsfsfs1234 'rm /home/fscrypttest/subdir/blob.copy && test ! -e /home/fscrypttest/subdir/blob.copy'
 
    systemctl stop user@"$(id -u fscrypttest)".service 2>/dev/null || true
    homectl deactivate fscrypttest 2>/dev/null || true
    wait_for_state fscrypttest inactive
 
    # After deactivation the fscrypt-encrypted directory is locked, so cleartext file names should not be visible
    [[ ! -e "$MNT/fscrypttest/file1" ]]
 
    # Verify we actually use the v2 format
    local SLOT
    SLOT="$(getfattr --absolute-names --only-values -n trusted.fscrypt_slot0 "$MNT/fscrypttest")"
    [[ "${SLOT:0:4}" == "\$v2:" ]] || {
        echo "fscrypt slot 0 is not in v2 format: ${SLOT:0:32}"
        return 1
    }
 
    PASSWORD=fsfsfs1234 homectl activate fscrypttest
    [[ "$(fscrypt_run0 fsfsfs1234 'head -n1 /home/fscrypttest/file1')" == "hello fscrypt" ]]
    systemctl stop user@"$(id -u fscrypttest)".service 2>/dev/null || true
    homectl deactivate fscrypttest 2>/dev/null || true
    wait_for_state fscrypttest inactive
 
    homectl update fscrypttest --real-name="Fscrypt Test" --offline
    inspect fscrypttest | grep "Real Name: Fscrypt Test" >/dev/null
 
    PASSWORD=fsfsfs1234 NEWPASSWORD=newfsfs5678 homectl passwd fscrypttest
    PASSWORD=newfsfs5678 homectl authenticate fscrypttest
    (! PASSWORD=fsfsfs1234 timeout 10s homectl authenticate fscrypttest </dev/null)
 
    PASSWORD=newfsfs5678 homectl activate fscrypttest
    [[ "$(fscrypt_run0 newfsfs5678 'head -n1 /home/fscrypttest/file1')" == "hello fscrypt" ]]
    fscrypt_run0 newfsfs5678 'rm -r /home/fscrypttest/subdir /home/fscrypttest/file1'
    systemctl stop user@"$(id -u fscrypttest)".service 2>/dev/null || true
    homectl deactivate fscrypttest 2>/dev/null || true
    wait_for_state fscrypttest inactive
 
    homectl remove fscrypttest
}
 
testcase_deactivate_busy() {
    # Verify that "homectl deactivate" is robust against transient EBUSY
    # failures of the umount() inside systemd-homework. This used to make
    # TEST-46-HOMED occasionally fail when something briefly held a reference
    # to the home mount at the moment the deactivation tried to unmount it.
    #
    # Reproduce the situation deterministically by spawning a background
    # process whose cwd is the home directory: that holds the mount busy via
    # the kernel's cwd reference until the process exits, so the initial
    # umount2() call in homework will fail with EBUSY. homectl is expected to
    # transparently retry the bus call until it succeeds (once the holder
    # exits).
 
    NEWPASSWORD=hunter2 homectl create \
        --storage=directory \
        --enforce-password-policy=no \
        busytest
    PASSWORD=hunter2 homectl activate busytest
    inspect busytest
 
    # Make sure the home is actually mounted before we try to hold it busy,
    # otherwise the subshell below would silently fail to acquire the cwd
    # reference.
    mountpoint /home/busytest
 
    # Spawn a process whose cwd is inside the home mount. `cd` is a shell
    # builtin so the subshell process itself acquires the cwd reference, and
    # `exec sleep` then preserves it across the exec.
    ( cd /home/busytest && exec sleep 10 ) &
    local busy_pid=$!
 
    # Wait until the kernel actually reports the cwd of the background
    # process as the home directory, so we know the busy reference is in
    # place before we attempt to deactivate.
    timeout 5 bash -c "until [[ \"\$(readlink /proc/${busy_pid}/cwd 2>/dev/null)\" == /home/busytest ]]; do sleep 0.1; done"
 
    # The deactivate must succeed eventually: the first umount2() will fail
    # with EBUSY, but homectl retries the call for up to 30 seconds, by
    # which time the background process will have exited and released the
    # cwd reference.
    homectl deactivate busytest
    wait_for_state busytest inactive
 
    wait "$busy_pid" || true
    homectl remove busytest
}
 
testcase_identity_groups() {
    NEWPASSWORD=foobar homectl create idgrouptest --storage=directory --shell=/bin/bash --enforce-password-policy=no --rebalance-weight=off
    PASSWORD=foobar homectl activate idgrouptest
 
    machinectl shell idgrouptest@ /usr/bin/bash -euxo pipefail -c "jq '.memberOf = ((.memberOf // []) + [\"systemd-journal\"] | unique) | .lastChangeUSec = ((.lastChangeUSec // 0) + 3600000000)' /home/idgrouptest/.identity > /home/idgrouptest/.identity.new && mv -f /home/idgrouptest/.identity.new /home/idgrouptest/.identity"
    jq -e '.memberOf | index("systemd-journal") != null' /home/idgrouptest/.identity
    wait_for_state idgrouptest active
 
    PASSWORD=foobar homectl authenticate idgrouptest
 
    local groups
    groups="$(machinectl shell idgrouptest@ /usr/bin/groups)"
    (! grep systemd-journal <<<"$groups" >/dev/null)
 
    homectl deactivate idgrouptest ||:
    wait_for_state idgrouptest inactive
    homectl remove idgrouptest
 
    # Install a PK rule that allows 'idgrouptest2' user to update homed even
    # though they are not on an fg console, just for testing
    mkdir -p /etc/polkit-1/rules.d
    cat >/etc/polkit-1/rules.d/updatehome.rules <<'EOF'
polkit.addRule(function(action, subject) {
    if (action.id == "org.freedesktop.home1.update-home-by-owner" &&
        subject.user == "idgrouptest2") {
        return polkit.Result.YES;
    }
});
EOF
    trap 'rm -f /etc/polkit-1/rules.d/updatehome.rules' RETURN ERR EXIT
    systemctl try-reload-or-restart polkit.service
 
    NEWPASSWORD=foobar homectl create idgrouptest2 --storage=directory --shell=/bin/bash --enforce-password-policy=no --rebalance-weight=off
    PASSWORD=foobar homectl activate idgrouptest2
 
    cat >/tmp/idgrouptest-add-group.sh <<'EOF'
#!/bin/bash
set -exuo pipefail
 
loginctl show-session "${XDG_SESSION_ID:?}" -p Active --value | grep '^yes$' >/dev/null
RECORD="$(busctl -j call org.freedesktop.home1 /org/freedesktop/home1 org.freedesktop.home1.Manager GetUserRecordByName s "$USER" | jq -r '.data[0]')"
TS="$(printf '%s' "$RECORD" | jq '.lastChangeUSec')"
NEW_TS=$((TS + 172800000000))
jq --arg g systemd-journal --argjson ts "$NEW_TS" \
   '.memberOf = ((.memberOf // []) + [$g]) | .lastChangeUSec = $ts' \
   ~/.identity > ~/.identity.new
mv -f ~/.identity.new ~/.identity
# Ensure the identity update is persisted before UpdateHomeEx reads it.
sync
 
UPDATE_TS=$((TS + 1))
UPDATE_RECORD="$(printf '%s' "$RECORD" | jq -c --argjson ts "$UPDATE_TS" --arg p foobar 'del(.binding, .status, .signature) | .lastChangeUSec = $ts | .secret = {password: [$p]}')"
(! busctl call org.freedesktop.home1 /org/freedesktop/home1 org.freedesktop.home1.Manager UpdateHomeEx "sa{sh}t" "$UPDATE_RECORD" 0 0 )
EOF
    chmod +x /tmp/idgrouptest-add-group.sh
    machinectl shell idgrouptest2@ /tmp/idgrouptest-add-group.sh
    rm -f /tmp/idgrouptest-add-group.sh
 
    PASSWORD=foobar homectl authenticate idgrouptest2
 
    local groups
    groups="$(machinectl shell idgrouptest2@ /usr/bin/groups)"
    (! grep systemd-journal <<<"$groups" >/dev/null)
 
    homectl deactivate idgrouptest2 ||:
    wait_for_state idgrouptest2 inactive
    homectl remove idgrouptest2
}
 
run_testcases