termux-packages/packages/termux-tools/termux-info
Leonid Pliushch dc14c12940 apt: prevent usage of certain commands as root
Usage of package manager as root has certain bad effects in Termux such as
messed up SELinux contexts and ownership. Root checks done in 'pkg' wrapper
are not reliable because one can execute 'apt' directly or with third-party
script downloaded from the Internet.

This commit adds user id check and if it found that uid is 0, apt will
refuse to do any work in root session. These checks done in such way so
they cannot be bypassed in any way unless command is executed as non-root
user.

Those who use Termux via ADB root shell should be able to switch to Termux
user id with command 'su' in order to have package manager working.

---

This change also affects the 'termux-info' utility:

 * It will no longer use 'apt policy' to detect subscribed repositories. Each
   source will be checked by script manually.

 * Information will be copied to clipboard only if 'termux-api' is installed.

 * Syntax error in timeout command is fixed: 'timeout' doesn't understand the
   argument '-t'.

 * Minor information entries reordering.
2019-11-13 16:38:40 +02:00

75 lines
1.8 KiB
Bash
Executable File

#!/bin/bash
if [ "$#" != "0" ]; then
echo 'usage: termux-info'
echo 'Provides information about Termux, and the current system. Helpful for debugging.'
exit
fi
updates() {
local updatable
if [ "$(id -u)" = "0" ]; then
echo "Running as root. Cannot check package updates."
else
apt update >/dev/null 2>&1
updatable=$(apt list --upgradable 2>/dev/null | tail -n +2)
if [ -z "$updatable" ];then
echo "All packages up to date"
else
echo "$updatable"
fi
fi
}
repo_subscriptions() {
local main_sources
main_sources=$(grep -P '^\s*deb\s' "@TERMUX_PREFIX@/etc/apt/sources.list")
if [ -n "$main_sources" ]; then
echo "# sources.list"
echo "$main_sources"
fi
local filename repo_package supl_sources
while read -r filename; do
repo_package=$(dpkg -S "$filename" 2>/dev/null | cut -d : -f 1)
supl_sources=$(grep -P '^\s*deb\s' "$filename")
if [ -n "$supl_sources" ]; then
if [ -n "$repo_package" ]; then
echo "# $repo_package (sources.list.d/$(basename "$filename"))"
else
echo "# sources.list.d/$(basename "$filename")"
fi
echo "$supl_sources"
fi
done < <(find "@TERMUX_PREFIX@/etc/apt/sources.list.d" -maxdepth 1 ! -type d)
}
output="Packages CPU architecture:
$(dpkg --print-architecture)
Subscribed repositories:
$(repo_subscriptions)
Updatable packages:
$(updates)
Android version:
$(getprop ro.build.version.release)
Kernel build information:
$(uname -a)
Device manufacturer:
$(getprop ro.product.manufacturer)
Device model:
$(getprop ro.product.model)"
echo "$output"
if [ -n "$(command -v termux-clipboard-set)" ]; then
# Copy to clipboard (requires termux-api)
# use timeout in case termux-api is installed but the termux:api app is missing
echo "$output" | timeout 3 termux-clipboard-set 2>/dev/null
timeout 3 termux-toast "Information has been copied to the clipboard" 2>/dev/null
fi
exit 0