Skip to content

Commit 913c22f

Browse files
authored
Update installation script to support doas (#1042)
1 parent 686c4b0 commit 913c22f

File tree

1 file changed

+52
-44
lines changed

1 file changed

+52
-44
lines changed

src/install/default.txt

Lines changed: 52 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#
1515
# OPTIONS: -p, --prefix "${INSTALL_PREFIX}"
1616
# Prefix to install croc into. Defaults to /usr/local/bin
17-
# REQUIREMENTS: bash, uname, tar/unzip, curl/wget, sudo (if not run
17+
# REQUIREMENTS: bash, uname, tar/unzip, curl/wget, sudo/doas (if not run
1818
# as root), install, mktemp, sha256sum/shasum/sha256
1919
#
2020
# BUGS: ...hopefully not. Please report.
@@ -202,6 +202,33 @@ determine_arch() {
202202
fi
203203
}
204204

205+
#--- FUNCTION ----------------------------------------------------------------
206+
# NAME: run_as
207+
# DESCRIPTION: Run a command as root if needed. If already root, runs it
208+
# directly; otherwise tries sudo then doas. Returns 21 if
209+
# neither sudo nor doas is available when needed.
210+
# PARAMETERS: $@ = command and args
211+
# RETURNS: Return code of executed command, or 21 if no escalation tool
212+
#-------------------------------------------------------------------------------
213+
run_as() {
214+
if [[ "${EUID}" == "0" ]]; then
215+
"$@"
216+
return $?
217+
fi
218+
219+
if command -v sudo >/dev/null 2>&1; then
220+
sudo "$@"
221+
return $?
222+
fi
223+
224+
if command -v doas >/dev/null 2>&1; then
225+
doas "$@"
226+
return $?
227+
fi
228+
229+
# No escalation tool found
230+
return 21
231+
}
205232

206233
#--- FUNCTION ----------------------------------------------------------------
207234
# NAME: download_file
@@ -342,13 +369,13 @@ extract_file() {
342369
#--- FUNCTION ----------------------------------------------------------------
343370
# NAME: create_prefix
344371
# DESCRIPTION: Creates the install prefix (and any parent directories). If
345-
# EUID not 0, then attempt to use sudo.
372+
# EUID not 0, then attempt to use sudo/doas (run_as).
346373
# PARAMETERS: $1 = prefix
347374
# RETURNS: Return code of the tool used to make the directory
348375
# 0 = Created the directory
349376
# >0 = Failed to create directory
350377
# 20 = Could not find mkdir command
351-
# 21 = Could not find sudo command
378+
# 21 = Could not find sudo/doas command (when needed)
352379
#-------------------------------------------------------------------------------
353380
create_prefix() {
354381
local prefix
@@ -357,17 +384,8 @@ create_prefix() {
357384
prefix="${1}"
358385

359386
if command -v mkdir >/dev/null 2>&1; then
360-
if [[ "${EUID}" == "0" ]]; then
361-
mkdir -p "${prefix}"
362-
rcode="${?}"
363-
else
364-
if command -v sudo >/dev/null 2>&1; then
365-
sudo mkdir -p "${prefix}"
366-
rcode="${?}"
367-
else
368-
rcode="21"
369-
fi
370-
fi
387+
run_as mkdir -p "${prefix}"
388+
rcode="${?}"
371389
else
372390
rcode="20"
373391
fi
@@ -378,13 +396,13 @@ create_prefix() {
378396
#--- FUNCTION ----------------------------------------------------------------
379397
# NAME: install_file_freebsd
380398
# DESCRIPTION: Installs a file into a location using 'install'. If EUID not
381-
# 0, then attempt to use sudo.
399+
# 0, then attempt to use sudo/doas (run_as).
382400
# PARAMETERS: $1 = file to install
383401
# $2 = location to install file into
384402
# RETURNS: 0 = File Installed
385403
# 1 = File not installed
386404
# 20 = Could not find install command
387-
# 21 = Could not find sudo command
405+
# 21 = Could not find sudo/doas command (when needed)
388406
#-------------------------------------------------------------------------------
389407
install_file_freebsd() {
390408
local file
@@ -395,17 +413,8 @@ install_file_freebsd() {
395413
prefix="${2}"
396414

397415
if command -v install >/dev/null 2>&1; then
398-
if [[ "${EUID}" == "0" ]]; then
399-
install -C -b -B '_old' -m 755 "${file}" "${prefix}"
400-
rcode="${?}"
401-
else
402-
if command -v sudo >/dev/null 2>&1; then
403-
sudo install -C -b -B '_old' -m 755 "${file}" "${prefix}"
404-
rcode="${?}"
405-
else
406-
rcode="21"
407-
fi
408-
fi
416+
run_as install -C -b -B '_old' -m 755 "${file}" "${prefix}"
417+
rcode="${?}"
409418
else
410419
rcode="20"
411420
fi
@@ -416,14 +425,14 @@ install_file_freebsd() {
416425
#--- FUNCTION ----------------------------------------------------------------
417426
# NAME: install_file_linux
418427
# DESCRIPTION: Installs a file into a location using 'install'. If EUID not
419-
# 0, then attempt to use sudo (unless on android).
428+
# 0, then attempt to use sudo/doas (run_as) (unless on android).
420429
# Falls back to cp/chmod for BusyBox compatibility.
421430
# PARAMETERS: $1 = file to install
422431
# $2 = location to install file into
423432
# RETURNS: 0 = File Installed
424433
# 1 = File not installed
425434
# 20 = Could not find install or cp command
426-
# 21 = Could not find sudo command
435+
# 21 = Could not find sudo/doas command (when needed)
427436
#-------------------------------------------------------------------------------
428437
install_file_linux() {
429438
local file
@@ -440,10 +449,15 @@ install_file_linux() {
440449
install -m 755 "${file}" "${prefix}/"
441450
rcode="${?}"
442451
else
452+
# Try sudo, then doas, then (special-case) Android direct install, else fail
443453
if command -v sudo >/dev/null 2>&1; then
444454
sudo install -C -b -S '_old' -m 755 "${file}" "${prefix}" 2>/dev/null || \
445455
sudo install -m 755 "${file}" "${prefix}/"
446456
rcode="${?}"
457+
elif command -v doas >/dev/null 2>&1; then
458+
doas install -C -b -S '_old' -m 755 "${file}" "${prefix}" 2>/dev/null || \
459+
doas install -m 755 "${file}" "${prefix}/"
460+
rcode="${?}"
447461
elif [[ "${ANDROID_ROOT}" != "" ]]; then
448462
install -C -b -S '_old' -m 755 -t "${prefix}" "${file}" 2>/dev/null || \
449463
install -m 755 "${file}" "${prefix}/"
@@ -461,6 +475,9 @@ install_file_linux() {
461475
if command -v sudo >/dev/null 2>&1; then
462476
sudo cp "${file}" "${prefix}/" && sudo chmod 755 "${prefix}/$(basename "${file}")"
463477
rcode="${?}"
478+
elif command -v doas >/dev/null 2>&1; then
479+
doas cp "${file}" "${prefix}/" && doas chmod 755 "${prefix}/$(basename "${file}")"
480+
rcode="${?}"
464481
else
465482
rcode="21"
466483
fi
@@ -475,13 +492,13 @@ install_file_linux() {
475492
#--- FUNCTION ----------------------------------------------------------------
476493
# NAME: install_file_cygwin
477494
# DESCRIPTION: Installs a file into a location using 'install'. If EUID not
478-
# 0, then attempt to use sudo.
495+
# 0, then attempt to use sudo/doas (run_as).
479496
# Not really 100% sure this is how to install croc in cygwin.
480497
# PARAMETERS: $1 = file to install
481498
# $2 = location to install file into
482499
# RETURNS: 0 = File Installed
483500
# 20 = Could not find install command
484-
# 21 = Could not find sudo command
501+
# 21 = Could not find sudo/doas command (when needed)
485502
#-------------------------------------------------------------------------------
486503
install_file_cygwin() {
487504
local file
@@ -492,17 +509,8 @@ install_file_cygwin() {
492509
prefix="${2}"
493510

494511
if command -v install >/dev/null 2>&1; then
495-
if [[ "${EUID}" == "0" ]]; then
496-
install -m 755 "${prefix}" "${file}"
497-
rcode="${?}"
498-
else
499-
if command -v sudo >/dev/null 2>&1; then
500-
sudo install -m 755 "${file}" "${prefix}"
501-
rcode="${?}"
502-
else
503-
rcode="21"
504-
fi
505-
fi
512+
run_as install -m 755 "${file}" "${prefix}"
513+
rcode="${?}"
506514
else
507515
rcode="20"
508516
fi
@@ -701,7 +709,7 @@ main() {
701709
print_message "== Failed to find mkdir in path" "error"
702710
exit 1
703711
elif [[ "${create_prefix_rcode}" == "21" ]]; then
704-
print_message "== Failed to find sudo in path" "error"
712+
print_message "== Failed to find sudo or doas in path" "error"
705713
exit 1
706714
else
707715
print_message "== Failed to create the install prefix: ${prefix}" "error"
@@ -732,7 +740,7 @@ main() {
732740
print_message "== Failed to locate 'install' command" "error"
733741
exit 1
734742
elif [[ "${install_file_rcode}" == "21" ]]; then
735-
print_message "== Failed to locate 'sudo' command" "error"
743+
print_message "== Failed to locate 'sudo' or 'doas' command" "error"
736744
exit 1
737745
else
738746
print_message "== Install attempt returned an unexpected value of ${install_file_rcode}" "error"

0 commit comments

Comments
 (0)