-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsu
executable file
·144 lines (117 loc) · 3.04 KB
/
su
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/data/data/io.neoterm/files/usr/bin/bash
SU_LOCATIONS=("/system/bin/su" "/system/xbin/su" "/sbin/su" "/su/bin/su" "/magisk/.core/bin/su")
CURRENT_GID="$(id -g)"
CURRENT_UID="$(id -u)"
CURRENT_PWD="$(pwd)"
CURRENT_SHELL="$SHELL"
SELF="$(basename $0)"
TTY_BACKUP=""
VERSION="1.5"
function check_self() {
case "$SELF" in
su | sudo ) ;;
* ) SELF="su" ;;
esac
}
function die() {
echo "$SELF: $@" 1>&2
exit 1
}
function check_su() {
local su="$1"
if [[ ! -x "$su" ]]; then
die "No su binary found, (searched ${SU_LOCATIONS[@]})"
fi
}
function print_version() {
echo "$SELF: su wrapper for NeoTerm, version $VERSION"
local su="$(find_su_binary)"
check_su "$su"
echo "$su: su binary, version $(call_su --version)"
}
function find_su_binary() {
local su
for su in "${SU_LOCATIONS[@]}"; do
if [[ -x "$su" ]]; then
echo -n "$su"
return
fi
done
}
function generate_command() {
local callTERM="$TERM"
local callHome="$HOME"
local callPATH="$PATH:/system/bin:/system/xbin:/system/vendor/bin:/vendor/bin"
local callLD_LIB="$LD_LIBRARY_PATH:/system/lib64:/system/lib:/system/vendor/lib64:/system/vendor/lib:/vendor/lib64:/vendor/lib"
echo "export TERM=$TERM;export PATH=$callPATH;export LD_LIBRARY_PATH=$callLD_LIB;cd \"$CURRENT_PWD\""
}
function call_su() {
local su="$(find_su_binary)"
check_su "$su"
local commands="$@"
local args=
if "$su" --help 2>&1 | grep "preserve-environment" &>/dev/null; then
args="--preserve-environment -c"
else
args="-c"
fi
pre_call_su
# Clear NeoTerm libs
# Some su may crash if we do not do this
# eg. MagiskSU
env \
PATH="/system/bin:/system/xbin:/system/vendor/bin:/vendor/bin" \
LD_LIBRARY_PATH=/system/lib64:/system/lib:/system/vendor/lib64:/system/vendor/lib:/vendor/lib64:/vendor/lib \
$su $args "$commands"
post_call_su
}
function pre_call_su() {
# Save tty status
TTY_BACKUP="$(stty -g)"
}
function post_call_su() {
stty "$TTY_BACKUP"
}
function main() {
${SELF}_main "$@"
}
function su_help() {
echo "Usage: su [-] [-l|--login] [-c commands]"
}
function su_main() {
local arg
local commands
local login
while [[ "$#" > 0 ]]; do
arg="$1"; shift
case "$arg" in
"--help" ) su_help; exit 0 ;;
"-c" | "--command" ) commands="$@"; break ;;
"-" | "-l" | "--login" ) login=true ;;
"-v" | "--version" ) print_version; exit 0 ;;
esac
done
local args
if [[ "$commands" != "" ]]; then
args+="$(generate_command);$commands"
else
args+="$(generate_command);$CURRENT_SHELL"
fi
if [[ "$login" == true ]]; then
args+=" -"
fi
call_su "$args"
}
function sudo_help() {
echo "Usage: sudo <commands> [args]"
}
function sudo_main() {
if [[ "$#" == 0 ]]; then
sudo_help
exit 0
fi
local args="$@"
su_main -c "$args"
}
check_self
main "$@"