Skip to content

Commit abc35f6

Browse files
committed
Fix bugs in dist-installer-cli, rebuild dist-installer-cli-standalone
1 parent e8e8f4c commit abc35f6

File tree

2 files changed

+614
-355
lines changed

2 files changed

+614
-355
lines changed

usr/bin/dist-installer-cli

Lines changed: 112 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -355,9 +355,14 @@ get_independent_host_pkgs() {
355355
## Platform independent packages
356356
if has signify-openbsd; then
357357
## fix Debian unconventional naming
358-
signify() {
358+
run_signify() {
359359
# shellcheck disable=SC2317
360-
signify-openbsd "${@}"
360+
${user_switch_prefix}/usr/bin/signify-openbsd "${@}"
361+
}
362+
else
363+
run_signify() {
364+
# shellcheck disable=SC2317
365+
${user_switch_prefix}/usr/bin/signify "${@}"
361366
}
362367
fi
363368

@@ -939,7 +944,7 @@ File '$file_name_item' exists but there is no associated VM registered in Virtua
939944

940945
vm_delete_kicksecure() {
941946
if [ "${vm_or_vms_already_existing_test_result}" = "true" ]; then
942-
log_run notice "${user_switch_prefix}vboxmanage" unregistervm "${guest_full_vm_name_kicksecure}" --delete
947+
log_run notice ${user_switch_prefix}vboxmanage unregistervm "${guest_full_vm_name_kicksecure}" --delete
943948
else
944949
log notice "VM Deletion: Kicksecure VM does not exist, no need to delete, ok."
945950
fi
@@ -948,7 +953,7 @@ vm_delete_kicksecure() {
948953

949954
vm_delete_gateway() {
950955
if [ "${gateway_exists}" = "1" ]; then
951-
log_run notice "${user_switch_prefix}vboxmanage" unregistervm "${guest_full_vm_name_gateway}" --delete
956+
log_run notice ${user_switch_prefix}vboxmanage unregistervm "${guest_full_vm_name_gateway}" --delete
952957
else
953958
log notice "VM Deletion: Gateway VM does not exist, no need to delete, ok."
954959
fi
@@ -957,7 +962,7 @@ vm_delete_gateway() {
957962

958963
vm_delete_workstation() {
959964
if [ "${workstation_exists}" = "1" ]; then
960-
log_run notice "${user_switch_prefix}vboxmanage" unregistervm "${guest_full_vm_name_workstation}" --delete
965+
log_run notice ${user_switch_prefix}vboxmanage unregistervm "${guest_full_vm_name_workstation}" --delete
961966
else
962967
log notice "VM Deletion: Workstation VM does not exist, no need to delete, ok."
963968
fi
@@ -1879,7 +1884,7 @@ install_oracle_repository_fedora() {
18791884
log info "Oracle Repository: Key /etc/pki/rpm-gpg/RPM-GPG-KEY-oracle already exists."
18801885
else
18811886
echo "${oracle_pgp}" | \
1882-
tee "${log_dir_cur}/oracle-virtualbox-2016.asc" >/dev/null
1887+
${user_switch_prefix}tee "${log_dir_cur}/oracle-virtualbox-2016.asc" >/dev/null
18831888
echo "${oracle_pgp}" | \
18841889
root_cmd tee /etc/pki/rpm-gpg/RPM-GPG-KEY-oracle >/dev/null
18851890
## Optional: the key will be imported when trying to use the repository
@@ -1917,7 +1922,7 @@ install_oracle_repository_debian() {
19171922
log info "Oracle Repository: Key /usr/share/keyrings/oracle.asc already exists."
19181923
else
19191924
echo "${oracle_pgp}" | \
1920-
tee "${log_dir_cur}/oracle-virtualbox-2016.asc" >/dev/null
1925+
${user_switch_prefix}tee "${log_dir_cur}/oracle-virtualbox-2016.asc" >/dev/null
19211926
echo "${oracle_pgp}" | \
19221927
root_cmd tee /usr/share/keyrings/oracle-virtualbox-2016.asc >/dev/null
19231928
fi
@@ -2446,7 +2451,7 @@ get_system_stat() {
24462451
fi
24472452
fi
24482453

2449-
df_output="$(df --output=avail -BG "${directory_prefix}")"
2454+
df_output="$(${user_switch_prefix}df --output=avail -BG "${directory_prefix}")"
24502455
free_space_available="$(echo "$df_output" |
24512456
awk '/G$/{print substr($1, 1, length($1)-1)}')"
24522457

@@ -2721,28 +2726,52 @@ get_file() {
27212726

27222727

27232728
test_file() {
2724-
local mode file
2725-
mode="${1}";
2726-
file="${2}";
2729+
local mode file real_file retcode
2730+
mode="${1}"
2731+
file="${2}"
2732+
## realpath needs to be able to access the path it checks
2733+
real_file="$(sudo realpath "${file}")"
2734+
## find needs to start execution in a directory it can access
2735+
pushd /
27272736
if [ "${mode}" = '-f' ]; then
2728-
if [ -z "$("${user_switch_prefix}find" "${file}" -maxdepth 0 -follow -type f)" ];
2729-
then return 1
2730-
else return 0
2737+
if [ -z "$(${user_switch_prefix}find "${file}" -maxdepth 0 -follow -type f)" ]; then
2738+
popd
2739+
return 1
2740+
else
2741+
popd
2742+
return 0
27312743
fi
27322744
elif [ "${mode}" = '-d' ]; then
2733-
if [ -z "$("${user_switch_prefix}find" "${file}" -maxdepth 0 -follow -type d)" ];
2734-
then return 1
2735-
else return 0
2745+
if [ -z "$(${user_switch_prefix}find "${file}" -maxdepth 0 -follow -type d)" ];
2746+
then
2747+
popd
2748+
return 1
2749+
else
2750+
popd
2751+
return 0
27362752
fi
27372753
elif [ "${mode}" = '-w' ]; then
2738-
"${user_switch_prefix}test" -w "${file}"
2754+
${user_switch_prefix}test -w "${file}"
2755+
retcode="$?"
2756+
popd
2757+
return "${retcode}"
27392758
elif [ "${mode}" = '-r' ]; then
2740-
"${user_switch_prefix}test" -r "${file}"
2759+
${user_switch_prefix}test -r "${file}"
2760+
retcode="$?"
2761+
popd
2762+
return "${retcode}"
27412763
elif [ "${mode}" = '-e' ]; then
2742-
if [ -z "$("${user_switch_prefix}find" "${file}" -maxdepth 0)" ];
2743-
then return 1
2744-
else return 0
2764+
if [ -z "$(${user_switch_prefix}find "${file}" -maxdepth 0)" ];
2765+
then
2766+
popd
2767+
return 1
2768+
else
2769+
popd
2770+
return 0
27452771
fi
2772+
else
2773+
popd
2774+
return 1
27462775
fi
27472776
}
27482777

@@ -3104,7 +3133,7 @@ should_download() {
31043133
fi
31053134
if [ "${dry_run}" = "1" ]; then
31063135
log notice "Download: Creating download flag via '--dry-run' option."
3107-
log_run notice "${user_switch_prefix}touch" "${download_flag}"
3136+
log_run notice ${user_switch_prefix}touch "${download_flag}"
31083137
return 0
31093138
fi
31103139

@@ -3135,9 +3164,9 @@ check_signature() {
31353164
log info "Signify key:\n${signify_key}"
31363165
log info "Verifying file: '${signify_checksum_file}'"
31373166
signify_pub_file="${log_dir_cur}/${signify_signer}.pub"
3138-
echo "${signify_key}" | tee "${signify_pub_file}" >/dev/null
3167+
echo "${signify_key}" | ${user_switch_prefix}tee "${signify_pub_file}" >/dev/null
31393168

3140-
log_run info "${user_switch_prefix}signify" -V -p "${signify_pub_file}" \
3169+
log_run info run_signify -V -p "${signify_pub_file}" \
31413170
-m "${signify_checksum_file}" || return 1
31423171

31433172
log info "Signify Signature Verification: 'success'"
@@ -3153,38 +3182,43 @@ check_hash() {
31533182
log info "Checking SHA512 checksum: '${shafile}"
31543183
## $checkhash needs to be executed on the same folder as the compared file.
31553184
log info "Changing to directory: '${dir}'"
3156-
cd "${dir}"
3157-
# shellcheck disable=SC2086
3158-
log_run info ${user_switch_prefix}${checkhash} "${shafile}" || return 1
3185+
if [ -n "${user_switch_prefix}" ]; then
3186+
## user_switch_prefix="sudo -u user " (including space at end of string)
3187+
${user_switch_prefix}-D "${dir}" ${checkhash} "${shafile}" || return 1
3188+
else
3189+
cd "${dir}"
3190+
# shellcheck disable=SC2086
3191+
log_run info ${user_switch_prefix}${checkhash} "${shafile}" || return 1
3192+
fi
31593193
log info "SHA512 Hash Verification: 'success'"
31603194
}
31613195

31623196

31633197
check_signature_test() {
31643198
log info "Unit testing signature, expecting non-zero exit code."
3165-
"${user_switch_prefix}rm" -f "${directory_prefix}/${guest_file}.${guest_file_ext}.sha512sums.test"
3166-
"${user_switch_prefix}cp" "${directory_prefix}/${guest_file}.${guest_file_ext}.sha512sums" "${directory_prefix}/${guest_file}.${guest_file_ext}.sha512sums.test"
3167-
echo "" | "${user_switch_prefix}tee" "${directory_prefix}/${guest_file}.${guest_file_ext}.sha512sums.test" >/dev/null
3199+
${user_switch_prefix}rm -f "${directory_prefix}/${guest_file}.${guest_file_ext}.sha512sums.test"
3200+
${user_switch_prefix}cp "${directory_prefix}/${guest_file}.${guest_file_ext}.sha512sums" "${directory_prefix}/${guest_file}.${guest_file_ext}.sha512sums.test"
3201+
echo "" | ${user_switch_prefix}tee "${directory_prefix}/${guest_file}.${guest_file_ext}.sha512sums.test" >/dev/null
31683202
if ! check_signature "${directory_prefix}/${guest_file}.${guest_file_ext}.sha512sums.test" 2>/dev/null; then
3169-
"${user_switch_prefix}rm" -f "${directory_prefix}/${guest_file}.${guest_file_ext}.sha512sums.test"
3203+
${user_switch_prefix}rm -f "${directory_prefix}/${guest_file}.${guest_file_ext}.sha512sums.test"
31703204
log info "Received expected non-zero exit code from unit test."
31713205
else
3172-
"${user_switch_prefix}rm" -f "${directory_prefix}/${guest_file}.${guest_file_ext}.sha512sums.test"
3206+
${user_switch_prefix}rm -f "${directory_prefix}/${guest_file}.${guest_file_ext}.sha512sums.test"
31733207
die 104 "${underline}SHA512 Hash Verification (unit test):${nounderline} 'FAIL' - received a zero as exit code, expected non-zero."
31743208
fi
31753209
}
31763210

31773211

31783212
check_hash_test() {
31793213
log info "Unit testing checksum, expecting non-zero exit code."
3180-
"${user_switch_prefix}rm" -f "${directory_prefix}/${guest_file}.${guest_file_ext}.sha512sums.test"
3181-
"${user_switch_prefix}cp" "${directory_prefix}/${guest_file}.${guest_file_ext}.sha512sums" "${directory_prefix}/${guest_file}.${guest_file_ext}.sha512sums.test"
3182-
echo "" | "${user_switch_prefix}tee" "${directory_prefix}/${guest_file}.${guest_file_ext}.sha512sums.test" >/dev/null
3214+
${user_switch_prefix}rm -f "${directory_prefix}/${guest_file}.${guest_file_ext}.sha512sums.test"
3215+
${user_switch_prefix}cp "${directory_prefix}/${guest_file}.${guest_file_ext}.sha512sums" "${directory_prefix}/${guest_file}.${guest_file_ext}.sha512sums.test"
3216+
echo "" | ${user_switch_prefix}tee "${directory_prefix}/${guest_file}.${guest_file_ext}.sha512sums.test" >/dev/null
31833217
if ! check_hash "${directory_prefix}/${guest_file}.${guest_file_ext}.sha512sums.test" 2>/dev/null; then
3184-
"${user_switch_prefix}rm" -f "${directory_prefix}/${guest_file}.${guest_file_ext}.sha512sums.test"
3218+
${user_switch_prefix}rm -f "${directory_prefix}/${guest_file}.${guest_file_ext}.sha512sums.test"
31853219
log info "Received expected non-zero exit code from unit test."
31863220
else
3187-
"${user_switch_prefix}rm" -f "${directory_prefix}/${guest_file}.${guest_file_ext}.sha512sums.test"
3221+
${user_switch_prefix}rm -f "${directory_prefix}/${guest_file}.${guest_file_ext}.sha512sums.test"
31883222
die 104 "${underline}SHA512 Hash Verification (unit test):${nounderline} 'FAIL' - received a zero as exit code, expected non-zero."
31893223
fi
31903224
}
@@ -3207,7 +3241,7 @@ check_integrity() {
32073241
check_signature_test
32083242
check_hash_test
32093243

3210-
log_run info "${user_switch_prefix}touch" "${download_flag}"
3244+
log_run info ${user_switch_prefix}touch "${download_flag}"
32113245

32123246
log notice "Integrity Check Result: 'success'"
32133247
}
@@ -3457,11 +3491,12 @@ set_default() {
34573491
set_target_user_account() {
34583492
local user
34593493
user="${1:-}"
3460-
if ! getent passwd "${user}" >/dev/null; then
3461-
die 1 "Specified user '${user}' does not exist."
3462-
fi
34633494
user_switch_prefix="sudo -u ${user} "
3464-
user_home_dir="$(getent passwd "${user}" | cut -d':' -f6)"
3495+
## getent may fail if the user doesn't exist, however we ignore it for now.
3496+
## The reason is because if this is run as a user account `sysmaint` on a
3497+
## system with no user `user`, this will fail, but the user may have passed
3498+
## their own --user and --directory-prefix flags which will fix it.
3499+
user_home_dir="$(getent passwd "${user}" | cut -d':' -f6)" || true
34653500
vboxmanage_locale_english="${user_switch_prefix}${vboxmanage_locale_english}"
34663501
}
34673502

@@ -3471,21 +3506,12 @@ adjust_default_for_sysmaint_maybe() {
34713506
log info 'Sysmaint user present, checking current user name...'
34723507

34733508
if [ "$(id -un)" = 'sysmaint' ]; then
3474-
log info "Running as sysmaint, adjusting behavior if needed."
3475-
3476-
if [ -z "${user_switch_prefix}" ]; then
3477-
if ! getent passwd user >/dev/null; then
3478-
die 1 "User 'user' does not exist, please set --user manually."
3479-
fi
3480-
set_target_user_account 'user'
3481-
fi
3509+
log info "Running as sysmaint, adjusting behavior."
34823510

3483-
if [ "${directory_prefix}" = "${HOME}/dist-installer-cli-download" ]; then
3484-
if [ ! -d "${user_home_dir}" ]; then
3485-
die 1 "${user_home_dir} does not exist, something is seriously wrong."
3486-
fi
3487-
directory_prefix="${user_home_dir}/dist-installer-cli-download"
3488-
fi
3511+
set_target_user_account 'user'
3512+
directory_prefix="${user_home_dir}/dist-installer-cli-download"
3513+
else
3514+
log info 'Not running as sysmaint user, ok.'
34893515
fi
34903516
else
34913517
log info 'Sysmaint user not present, ok.'
@@ -3553,6 +3579,23 @@ parse_name() {
35533579
}
35543580

35553581

3582+
copy_thru_barrier() {
3583+
local source dest source_basename dest_realpath dest_file
3584+
3585+
source="${1}"
3586+
dest="${2}"
3587+
source_basename="$(basename "${source}")"
3588+
dest_realpath="$(sudo realpath "${dest}")"
3589+
if test_file -d "${dest_realpath}"; then
3590+
dest_file="${dest_realpath}/${source_basename}"
3591+
else
3592+
dest_file="${dest_realpath}"
3593+
fi
3594+
3595+
${user_switch_prefix}tee "${dest_file}" > /dev/null < "${source}"
3596+
}
3597+
3598+
35563599
## Parse command-line options.
35573600
parse_opt() {
35583601
local directory_prefix_parent last_run_integer cur_run_integer log_dir_main
@@ -3715,7 +3758,7 @@ parse_opt() {
37153758
## is set to '~/', the parent '/home' is not writable.
37163759

37173760
log info "Creating directory: '${directory_prefix}'"
3718-
mkdir -p "${directory_prefix}" ||
3761+
${user_switch_prefix}mkdir -p "${directory_prefix}" ||
37193762
die 1 "Failed to created directory: '${directory_prefix}'"
37203763
test_file -w "${directory_prefix}" ||
37213764
die 1 "Directory isn't writable: '${directory_prefix}'"
@@ -3738,9 +3781,9 @@ parse_opt() {
37383781

37393782
## If the commands below fail, it should have failed earlier for the
37403783
## parent directory permissions, not below.
3741-
"${user_switch_prefix}mkdir" -p "${log_dir_cur}"
3742-
"${user_switch_prefix}cp" "${0}" "${log_dir_cur}"
3743-
"${user_switch_prefix}touch" "${log_file_user}"
3784+
${user_switch_prefix}mkdir -p "${log_dir_cur}"
3785+
copy_thru_barrier "${0}" "${log_dir_cur}";
3786+
${user_switch_prefix}touch "${log_file_user}"
37443787
fi
37453788

37463789
# shellcheck disable=SC2194
@@ -3785,15 +3828,15 @@ log_term_and_file() {
37853828
return 0
37863829
fi
37873830

3788-
touch "${log_file_user}"
3789-
touch "${log_file_debug}"
3831+
${user_switch_prefix}touch "${log_file_user}"
3832+
${user_switch_prefix}touch "${log_file_debug}"
37903833
## By appending '&' at the end, log_file_user would remain empty.
3791-
true "exec > >(tee -a \"${log_file_user}\") 2> >(tee -a \"${log_file_debug}\" >&2)"
3792-
exec > >(tee -a "${log_file_user}") 2> >(tee -a "${log_file_debug}" >&2)
3834+
true "exec > >(${user_switch_prefix}tee -a \"${log_file_user}\") 2> >(${user_switch_prefix}tee -a \"${log_file_debug}\" >&2)"
3835+
exec > >(${user_switch_prefix}tee -a "${log_file_user}") 2> >(${user_switch_prefix}tee -a "${log_file_debug}" >&2)
37933836
## Bash has built-in feature to redirect xtrace to the specified file.
37943837
# shellcheck disable=SC2039
3795-
true "exec 9>>${log_file_debug}"
3796-
exec 9>>"${log_file_debug}"
3838+
true "exec 9> >(${user_switch_prefix}tee -a \"${log_file_debug}\")"
3839+
exec 9> >(${user_switch_prefix}tee -a "${log_file_debug}")
37973840
export BASH_XTRACEFD=9
37983841
set -o xtrace
37993842
xtrace=1
@@ -3816,11 +3859,11 @@ run_installer() {
38163859
set_trap
38173860
## Parse script name for wanted values.
38183861
parse_name
3819-
## Parse command-line options.
3820-
parse_opt "${@}"
38213862
## If sysmaint is in use and the user hasn't configured their own directory
38223863
## prefix and target user, default to target account 'user'.
38233864
adjust_default_for_sysmaint_maybe
3865+
## Parse command-line options.
3866+
parse_opt "${@}"
38243867
## Logging mechanism.
38253868
log_term_and_file
38263869

0 commit comments

Comments
 (0)