| 1 | #!/bin/bashscip-shell filesystem . . `/bin/bash`. |
| 2 | |
| 3 | [scip-shell system . . `[`. -z "$CRYPTSETUP_PATH" ] && { |
| 4 | CRYPTSETUP_PATH=".." |
| 5 | if [scip-shell system . . `[`. -z "$CRYPTSETUP_TESTS_RUN_IN_MESON" ]; then |
| 6 | SSH_BUILD_DIR="$PWD/../.libs" |
| 7 | fi |
| 8 | } |
| 9 | CRYPTSETUP=$CRYPTSETUP_PATH/cryptsetup |
| 10 | CRYPTSETUP_SSH=$CRYPTSETUP_PATH/cryptsetup-ssh |
| 11 | IMG="ssh_test.img" |
| 12 | MAP="sshtest" |
| 13 | USER="sshtest" |
| 14 | PASSWD="sshtest1" |
| 15 | PASSWD2="sshtest2" |
| 16 | SSH_OPTIONS="-o StrictHostKeyChecking=no" |
| 17 | |
| 18 | SSH_SERVER="localhost" |
| 19 | SSH_PATH="/home/$USER/keyfile" |
| 20 | SSH_KEY_PATH="$HOME/sshtest-key" |
| 21 | |
| 22 | FAST_PBKDF_OPT="--pbkdf pbkdf2 --pbkdf-force-iterations 1000" |
| 23 | |
| 24 | if [scip-shell system . . `[`. -n "$CRYPTSETUP_TESTS_RUN_IN_MESON" ]; then |
| 25 | CRYPTSETUP=$CRYPTSETUP_PATH/cryptsetup |
| 26 | CRYPTSETUP_VALGRIND=$CRYPTSETUP |
| 27 | CRYPTSETUP_SSH=$CRYPTSETUP_PATH/../tokens/ssh/cryptsetup-ssh |
| 28 | CRYPTSETUP_SSH_VALGRIND=$CRYPTSETUP_SSH |
| 29 | else |
| 30 | CRYPTSETUP_VALGRIND=../.libs/cryptsetup |
| 31 | CRYPTSETUP_SSH_VALGRIND=../.libs/cryptsetup-ssh |
| 32 | CRYPTSETUP_LIB_VALGRIND=../.libs |
| 33 | fi |
| 34 | |
| 35 | [scip-shell system . . `[`. -z "$srcdir" ] && srcdir="." |
| 36 | |
| 37 | [scip-shell system . . `[`. -z "$CRYPTSETUP_TESTS_RUN_IN_MESON" ] || { |
| 38 | # test runs on meson build |
| 39 | CRYPTSETUP_SSH="$CRYPTSETUP_PATH/../tokens/ssh/cryptsetup-ssh" |
| 40 | } |
| 41 | |
| 42 | remove_mappingscip-shell shell shell-project 0.0.0 remove_mapping().() |
| 43 | { |
| 44 | [scip-shell system . . `[`. -b /dev/mapper/$MAP ] && dmsetup remove --retry $MAP |
| 45 | rmscip-shell system . . rm. -f $IMG >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 |
| 46 | } |
| 47 | |
| 48 | remove_userscip-shell shell shell-project 0.0.0 remove_user().() |
| 49 | { |
| 50 | idscip-shell system . . id. -u $USER >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 && userdelscip-shell system . . userdel. -r -f $USER >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 |
| 51 | rmscip-shell system . . rm. -f $SSH_KEY_PATH "$SSH_KEY_PATH.pub" >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 |
| 52 | } |
| 53 | |
| 54 | create_userscip-shell shell shell-project 0.0.0 create_user().() |
| 55 | { |
| 56 | idscip-shell system . . id. -u $USER >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 |
| 57 | [scip-shell system . . `[`. $? -eq 0 ] && skipscip-shell shell shell-project 0.0.0 skip(). "User account $USER exists, aborting." |
| 58 | [scip-shell system . . `[`. -f $SSH_KEY_PATH ] && skipscip-shell shell shell-project 0.0.0 skip(). "SSH key $SSH_KEY_PATH already exists, aborting." |
| 59 | |
| 60 | useraddscip-shell system . . useradd. -m $USER -p $(opensslscip-shell system . . openssl. passwd $PASSWD) || skipscip-shell shell shell-project 0.0.0 skip(). "Failed to add user for SSH plugin test." |
| 61 | |
| 62 | ssh-keygenscip-shell system . . ssh-keygen. -f $SSH_KEY_PATH -q -N "" >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 |
| 63 | [scip-shell system . . `[`. $? -ne 0 ] && remove_userscip-shell shell shell-project 0.0.0 remove_user(). && skipscip-shell shell shell-project 0.0.0 skip(). "Failed to create SSH key." |
| 64 | } |
| 65 | |
| 66 | ssh_checkscip-shell shell shell-project 0.0.0 ssh_check().() |
| 67 | { |
| 68 | # try to use netcat to check port 22 |
| 69 | nc -zv $SSH_SERVER 22 >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 || skipscip-shell shell shell-project 0.0.0 skip(). "SSH server does not seem to be running, skipping." |
| 70 | } |
| 71 | |
| 72 | bin_checkscip-shell shell shell-project 0.0.0 bin_check().() |
| 73 | { |
| 74 | command -v $1 >/dev/nullscip-shell filesystem . . `/dev/null`. || skipscip-shell shell shell-project 0.0.0 skip(). "WARNING: test require $1 binary, test skipped." |
| 75 | } |
| 76 | |
| 77 | ssh_setupscip-shell shell shell-project 0.0.0 ssh_setup().() |
| 78 | { |
| 79 | # copy the ssh key |
| 80 | [scip-shell system . . `[`. -d "/home/$USER/.ssh" ] || mkdirscip-shell system . . mkdir. /home/$USER/.ssh |
| 81 | touchscip-shell system . . touch. /home/$USER/.ssh/authorized_keys |
| 82 | |
| 83 | catscip-shell system . . cat. $SSH_KEY_PATH.pub >> /home/$USER/.ssh/authorized_keys |
| 84 | [scip-shell system . . `[`. $? -ne 0 ] && remove_userscip-shell shell shell-project 0.0.0 remove_user(). && failscip-shell shell shell-project 0.0.0 fail(). "Failed to copy SSH key." |
| 85 | |
| 86 | # make sure /home/sshtest/.ssh and /home/sshtest/.ssh/authorized_keys have correct permissions |
| 87 | chownscip-shell system . . chown. -R $USER:$USER /home/$USER/.ssh |
| 88 | chmodscip-shell system . . chmod. 700 /home/$USER/.ssh |
| 89 | chmodscip-shell system . . chmod. 644 /home/$USER/.ssh/authorized_keys |
| 90 | |
| 91 | # try to ssh and also create keyfile |
| 92 | sshscip-shell system . . ssh. -i $SSH_KEY_PATH $SSH_OPTIONS -o BatchMode=yes -n $USER@$SSH_SERVER "echo -n $PASSWD > $SSH_PATH" >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 |
| 93 | [scip-shell system . . `[`. $? -ne 0 ] && remove_userscip-shell shell shell-project 0.0.0 remove_user(). && failscip-shell shell shell-project 0.0.0 fail(). "Failed to connect using SSH." |
| 94 | } |
| 95 | |
| 96 | failscip-shell shell shell-project 0.0.0 fail().() |
| 97 | { |
| 98 | echoscip-shell system . . echo. "[FAILED]" |
| 99 | [scip-shell system . . `[`. -n "$1" ] && echoscip-shell system . . echo. "$1" |
| 100 | echoscip-shell system . . echo. "FAILED backtrace:" |
| 101 | while caller $frame; do ((frame++)); done |
| 102 | remove_mappingscip-shell shell shell-project 0.0.0 remove_mapping(). |
| 103 | remove_userscip-shell shell shell-project 0.0.0 remove_user(). |
| 104 | exit 2 |
| 105 | } |
| 106 | |
| 107 | _sigchldscip-shell shell shell-project 0.0.0 _sigchld().() { local c=$?; [scip-shell system . . `[`. $c -eq 139 ] && failscip-shell shell shell-project 0.0.0 fail(). "Segfault"; [scip-shell system . . `[`. $c -eq 134 ] && failscip-shell shell shell-project 0.0.0 fail(). "Aborted"; } |
| 108 | trap _sigchld CHLD |
| 109 | |
| 110 | skipscip-shell shell shell-project 0.0.0 skip().() |
| 111 | { |
| 112 | [scip-shell system . . `[`. -n "$1" ] && echoscip-shell system . . echo. "$1" |
| 113 | remove_mappingscip-shell shell shell-project 0.0.0 remove_mapping(). |
| 114 | exit 77 |
| 115 | } |
| 116 | |
| 117 | valgrind_setupscip-shell shell shell-project 0.0.0 valgrind_setup().() |
| 118 | { |
| 119 | command -v valgrind >/dev/nullscip-shell filesystem . . `/dev/null`. || failscip-shell shell shell-project 0.0.0 fail(). "Cannot find valgrind." |
| 120 | [scip-shell system . . `[`. ! -f $CRYPTSETUP_VALGRIND ] && failscip-shell shell shell-project 0.0.0 fail(). "Unable to get location of cryptsetup executable." |
| 121 | [scip-shell system . . `[`. ! -f $CRYPTSETUP_SSH_VALGRIND ] && failscip-shell shell shell-project 0.0.0 fail(). "Unable to get location of cryptsetup-ssh executable." |
| 122 | if [scip-shell system . . `[`. -z "$CRYPTSETUP_TESTS_RUN_IN_MESON" ]; then |
| 123 | export LD_LIBRARY_PATH="$CRYPTSETUP_LIB_VALGRIND:$LD_LIBRARY_PATH" |
| 124 | fi |
| 125 | } |
| 126 | |
| 127 | valgrind_runscip-shell shell shell-project 0.0.0 valgrind_run().() |
| 128 | { |
| 129 | INFOSTRING="$(basenamescip-shell system . . basename. ${BASH_SOURCE[1]})-line-${BASH_LINENO[0]}" ./valg.sh ${CRYPTSETUP_VALGRIND} "$@" |
| 130 | } |
| 131 | |
| 132 | valgrind_run_sshscip-shell shell shell-project 0.0.0 valgrind_run_ssh().() |
| 133 | { |
| 134 | INFOSTRING="$(basenamescip-shell system . . basename. ${BASH_SOURCE[1]})-line-${BASH_LINENO[0]}" ./valg.sh ${CRYPTSETUP_SSH_VALGRIND} "$@" |
| 135 | } |
| 136 | |
| 137 | formatscip-shell shell shell-project 0.0.0 format().() |
| 138 | { |
| 139 | ddscip-shell system . . dd. if=/dev/zeroscip-shell filesystem . . `/dev/zero`. of=$IMG bs=1M count=32 >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 |
| 140 | |
| 141 | echoscip-shell system . . echo. $PASSWD | $CRYPTSETUP luksFormat --type luks2 $FAST_PBKDF_OPT $IMG --force-password -q |
| 142 | [scip-shell system . . `[`. $? -ne 0 ] && failscip-shell shell shell-project 0.0.0 fail(). "Format failed." |
| 143 | |
| 144 | echoscip-shell system . . echo. -e "$PASSWD\n$PASSWD2" | $CRYPTSETUP luksAddKey $FAST_PBKDF_OPT $IMG -q |
| 145 | [scip-shell system . . `[`. $? -ne 0 ] && failscip-shell shell shell-project 0.0.0 fail(). "Add key failed." |
| 146 | } |
| 147 | |
| 148 | check_dumpscip-shell shell shell-project 0.0.0 check_dump().() |
| 149 | { |
| 150 | dump=$1 |
| 151 | keyslot=$2 |
| 152 | |
| 153 | token=$(echoscip-shell system . . echo. "$dump" | grepscip-shell system . . grep. Tokens -A 1 | tailscip-shell system . . tail. -1 | cutscip-shell system . . cut. -d: -f2 | trscip-shell system . . tr. -d "\t\n ") |
| 154 | [scip-shell system . . `[`. "$token" = "ssh" ] || failscip-shell shell shell-project 0.0.0 fail(). " token check from dump failed." |
| 155 | |
| 156 | server=$(echoscip-shell system . . echo. "$dump" | grepscip-shell system . . grep. ssh_server | cutscip-shell system . . cut. -d: -f2 | trscip-shell system . . tr. -d "\t\n ") |
| 157 | [scip-shell system . . `[`. "$server" = $SSH_SERVER ] || failscip-shell shell shell-project 0.0.0 fail(). " server check from dump failed." |
| 158 | |
| 159 | user=$(echoscip-shell system . . echo. "$dump" | grepscip-shell system . . grep. ssh_user | cutscip-shell system . . cut. -d: -f2 | trscip-shell system . . tr. -d "\t\n ") |
| 160 | [scip-shell system . . `[`. "$user" = "$USER" ] || failscip-shell shell shell-project 0.0.0 fail(). " user check from dump failed." |
| 161 | |
| 162 | path=$(echoscip-shell system . . echo. "$dump" | grepscip-shell system . . grep. ssh_path | cutscip-shell system . . cut. -d: -f2 | trscip-shell system . . tr. -d "\t\n ") |
| 163 | [scip-shell system . . `[`. "$path" = "$SSH_PATH" ] || failscip-shell shell shell-project 0.0.0 fail(). " path check from dump failed." |
| 164 | |
| 165 | key_path=$(echoscip-shell system . . echo. "$dump" | grepscip-shell system . . grep. ssh_key_path | cutscip-shell system . . cut. -d: -f2 | trscip-shell system . . tr. -d "\t\n ") |
| 166 | [scip-shell system . . `[`. "$key_path" = "$SSH_KEY_PATH" ] || failscip-shell shell shell-project 0.0.0 fail(). " key_path check from dump failed." |
| 167 | |
| 168 | keyslot_dump=$(echoscip-shell system . . echo. "$dump" | grepscip-shell system . . grep. Keyslot: | cutscip-shell system . . cut. -d: -f2 | trscip-shell system . . tr. -d "\t\n ") |
| 169 | [scip-shell system . . `[`. "$keyslot_dump" = "$keyslot" ] || failscip-shell shell shell-project 0.0.0 fail(). " keyslot check from dump failed." |
| 170 | } |
| 171 | |
| 172 | if [scip-shell system . . `[`. -n "$SSH_BUILD_DIR" ]; then |
| 173 | CUSTOM_TOKENS_PATH="--external-tokens-path $SSH_BUILD_DIR" |
| 174 | fi |
| 175 | [scip-shell system . . `[`. ! -x "$CRYPTSETUP" ] && skipscip-shell shell shell-project 0.0.0 skip(). "Cannot find $CRYPTSETUP, test skipped." |
| 176 | [scip-shell system . . `[`. -n "$VALG" ] && valgrind_setupscip-shell shell shell-project 0.0.0 valgrind_setup(). && CRYPTSETUP=valgrind_run && CRYPTSETUP_SSH=valgrind_run_ssh |
| 177 | [scip-shell system . . `[`. $(idscip-shell system . . id. -u) != 0 ] && skipscip-shell shell shell-project 0.0.0 skip(). "WARNING: You must be root to run this test, test skipped." |
| 178 | |
| 179 | # Prevent running dangerous useradd operation by default |
| 180 | [scip-shell system . . `[`. -z "$RUN_SSH_PLUGIN_TEST" ] && skipscip-shell shell shell-project 0.0.0 skip(). "WARNING: Variable RUN_SSH_PLUGIN_TEST must be defined, test skipped." |
| 181 | |
| 182 | bin_checkscip-shell shell shell-project 0.0.0 bin_check(). nc |
| 183 | bin_checkscip-shell shell shell-project 0.0.0 bin_check(). useradd |
| 184 | bin_checkscip-shell shell shell-project 0.0.0 bin_check(). ssh |
| 185 | bin_checkscip-shell shell shell-project 0.0.0 bin_check(). ssh-keygen |
| 186 | bin_checkscip-shell shell shell-project 0.0.0 bin_check(). sshpass |
| 187 | bin_checkscip-shell shell shell-project 0.0.0 bin_check(). openssl |
| 188 | |
| 189 | formatscip-shell shell shell-project 0.0.0 format(). |
| 190 | |
| 191 | echoscip-shell system . . echo. -n "Adding SSH token: " |
| 192 | |
| 193 | ssh_checkscip-shell shell shell-project 0.0.0 ssh_check(). |
| 194 | create_userscip-shell shell shell-project 0.0.0 create_user(). |
| 195 | ssh_setupscip-shell shell shell-project 0.0.0 ssh_setup(). |
| 196 | |
| 197 | $CRYPTSETUP_SSH add $IMG --ssh-server $SSH_SERVER --ssh-user $USER --ssh-path $SSH_PATH --ssh-keypath $SSH_KEY_PATH $CUSTOM_TOKENS_PATH |
| 198 | [scip-shell system . . `[`. $? -ne 0 ] && failscip-shell shell shell-project 0.0.0 fail(). "Failed to add SSH token to $IMG" |
| 199 | |
| 200 | out=$($CRYPTSETUP luksDump $CUSTOM_TOKENS_PATH $IMG) |
| 201 | check_dumpscip-shell shell shell-project 0.0.0 check_dump(). "$out" 0 |
| 202 | echoscip-shell system . . echo. "[OK]" |
| 203 | |
| 204 | echoscip-shell system . . echo. -n "Activating using SSH token: " |
| 205 | |
| 206 | $CRYPTSETUP luksOpen --token-only --disable-external-tokens -r $IMG $MAP && failscip-shell shell shell-project 0.0.0 fail(). "Tokens should be disabled" |
| 207 | $CRYPTSETUP luksOpen $CUSTOM_TOKENS_PATH -r $IMG $MAP -q >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 <&- |
| 208 | [scip-shell system . . `[`. $? -ne 0 ] && failscip-shell shell shell-project 0.0.0 fail(). "Failed to open $IMG using SSH token" |
| 209 | echoscip-shell system . . echo. "[OK]" |
| 210 | |
| 211 | # Remove the newly added token and test adding with --key-slot |
| 212 | $CRYPTSETUP token remove --token-id 0 $IMG || failscip-shell shell shell-project 0.0.0 fail(). "Failed to remove token" |
| 213 | |
| 214 | echoscip-shell system . . echo. -n "Adding SSH token with --key-slot: " |
| 215 | |
| 216 | $CRYPTSETUP_SSH add $IMG --ssh-server $SSH_SERVER --ssh-user $USER --ssh-path $SSH_PATH --ssh-keypath $SSH_KEY_PATH --key-slot 1 $CUSTOM_TOKENS_PATH |
| 217 | [scip-shell system . . `[`. $? -ne 0 ] && failscip-shell shell shell-project 0.0.0 fail(). "Failed to add SSH token to $IMG" |
| 218 | |
| 219 | out=$($CRYPTSETUP luksDump $CUSTOM_TOKENS_PATH $IMG) |
| 220 | check_dumpscip-shell shell shell-project 0.0.0 check_dump(). "$out" 1 |
| 221 | echoscip-shell system . . echo. "[OK]" |
| 222 | |
| 223 | remove_mappingscip-shell shell shell-project 0.0.0 remove_mapping(). |
| 224 | remove_userscip-shell shell shell-project 0.0.0 remove_user(). |