-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbitsenv
More file actions
executable file
·367 lines (323 loc) · 9.86 KB
/
bitsenv
File metadata and controls
executable file
·367 lines (323 loc) · 9.86 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#!/bin/bash
function Help() {
cat<<EOF
Usage: bitsenv [-m <modules directory>] [-p <platform>] [-h]
<checkenv|printenv|enter> <package/version>[,<package/version>...]
setenv> <package/version>[,<package/version>...] -c <command>
<query|q>
EOF
exit 0
}
version=3.2.10
prog=`readlink $0 2>/dev/null`
[[ -z $prog ]] && prog=$0
path=`dirname $prog`
path=`cd "$path";pwd`
[ x$BITSENV_DEBUG == x1 ] && printf "path=$path\nprog=$prog\n" >&2
path=$(cd $(dirname $0) && pwd) # appended to PATH later on
printvar=""
# If using modulecmd from the system, and ver >= 4, this var enables 3.x compat mode
export MODULES_USE_COMPAT_VERSION=1
Eval(){
cmd=$*
ret=`$cmd` || return 1
[ -z "$ret" ] && return 0
eval $ret
}
# SECURITY: Do NOT source .bitsenv from the current working directory.
# Sourcing a file from CWD allows arbitrary code execution if an attacker
# can place a malicious .bitsenv in any directory the user might cd into.
# Settings are instead picked up via the -m / -p flags or environment variables.
#[ -f .bitsenv ] && source .bitsenv # removed: CWD sourcing is a code-execution vector
argc=$#
argv=("$@")
for (( j=0; j<argc; j++ ))
do
case "${argv[j]}" in
-m|-?modules|-?mdir|-?moduledir)
moduledir="${argv[j+1]}"
;;
-p|-?platform)
platform="${argv[j+1]}"
;;
-h)
Help
;;
*)
;;
esac
done
if [[ -z "$platform" ]]; then
if [[ -n "$BITS_PLATFORM" ]]; then
printf "WARNING: overriding detected platform (%s) with %s\n" "$platform" "$BITS_PLATFORM" >&2
platform="$BITS_PLATFORM"
else
platform=$(bits architecture)
fi
fi
if [[ -z "$moduledir" ]]; then
if [[ -n "$BITS_MODULEDIR" ]]; then
moduledir="$BITS_MODULEDIR"
elif [[ "$(basename "$prog")" == bitsenv && "$(basename "$path")" == bin ]]; then
moduledir="$(dirname "$path")"
fi
fi
if [[ -z "$moduledir" ]]; then
bits=$(command -v bits 2>/dev/null)
if [[ -n "$bits" ]]; then
# Quote both $bits and $@ to prevent word-splitting on paths with spaces.
exec "$bits" "$@"
fi
printf "Could not determine module directory, please set BITS_MODULEDIR\n"
exit 1
fi
if [ -w . -a ! -f bits.rc -a ! -f .bitsrc ]
then
printf "moduledir=$moduledir\nplatform=$platform\n" > .bitsenv
fi
modules=$moduledir/$platform/Modules
if [ ! -d $modules ]
then
echo "$modules directory not found"
exit 1
fi
unset MODULESHOME
function modulepath() {
local dir
local colon
local subdir
subdir=$1; shift 1
for dir in $MODULEPATH $*
do
if [ -d $dir/Modules/$subdir ]
then
printf "${colon}${dir}/Modules/${subdir}"
colon=":"
fi
done
printf "\n"
}
function test_toolchain() {
# Use mktemp to avoid predictable /tmp paths that are vulnerable to
# symlink attacks and race conditions (CWE-377 / CWE-362).
local TMPDIR_TC
TMPDIR_TC=$(mktemp -d)
local TMPPREF="${TMPDIR_TC}/helloworld"
cat > "${TMPPREF}.cpp" <<'EOF'
#include <iostream>
int main(int, char *[]) {
std::cout << "hello world" << std::endl;
return 0;
}
EOF
g++ -o "$TMPPREF" "${TMPPREF}.cpp" > "${TMPPREF}.log" 2>&1
if [[ "$("$TMPPREF" 2>/dev/null)" != "hello world" ]]; then
echo "WARNING: We are using GNU C++ compiler at $(command -v g++ 2>/dev/null)" >&2
echo "WARNING: This compiler is unable to produce valid executables on this platform!" >&2
echo "WARNING: Error from g++ follows:" >&2
while IFS= read -r LINE; do
echo "WARNING: $LINE" >&2
done < "${TMPPREF}.log"
else
echo "NOTICE: loaded compiler ($(command -v g++)) seems to produce valid executables" >&2
fi
rm -rf "$TMPDIR_TC"
}
# Transform a comma-separated packages to modulecmd
# format, i.e.:
#
# Returned list (on stdout) is also sorted: packages with a certain priority
# are moved at the beginning of the list.
# Remove duplicate entries from a colon-separated path variable, preserving
# the order of first occurrence. Empty elements (artefacts of double-colons
# or leading/trailing colons) are also discarded.
#
# Usage: dedup_pathvar VARNAME (operates in-place, re-exports the variable)
function dedup_pathvar() {
local varname="$1"
local current="${!varname}"
[[ -z "$current" ]] && return 0
local result="" elem
local -A _seen # associative array used as a hash-set
local IFS=:
for elem in $current; do
[[ -z "$elem" ]] && continue # skip empty elements
if [[ -z "${_seen[$elem]+x}" ]]; then # not yet seen
_seen[$elem]=1
result="${result:+${result}:}${elem}"
fi
done
unset _seen
printf -v "$varname" '%s' "$result"
export "$varname"
}
# Deduplicate all path-like variables that accumulate duplicates when module
# environments are nested. DYLD_LIBRARY_PATH is the macOS equivalent of
# LD_LIBRARY_PATH; both are normalised alongside PATH.
function dedup_path_vars() {
local var
for var in PATH LD_LIBRARY_PATH DYLD_LIBRARY_PATH; do
[[ -n "${!var}" ]] && dedup_pathvar "$var"
done
[[ $BITSENV_DEBUG == 1 ]] && \
printf "NOTICE: PATH deduplicated: %s\n" "$PATH" >&2
}
function normalize_sort_packages() {
# Use pure bash string ops instead of piping through sed to avoid
# word-splitting and potential injection issues with package names.
local input="${1//,/ }" # commas → spaces
local token result=()
for token in $input; do
token="${token//::///}" # :: → /
result+=("$token")
done
[[ $BITSENV_DEBUG == 1 ]] && printf "NOTICE: list of packages normalized to %s\n" "${result[*]}" >&2
echo "${result[*]}"
}
# Extend PATH and immediately deduplicate all path-like variables so that
# any duplicates already present from outer (nested) module environments
# are collapsed before new entries are added.
export PATH=$PATH:$path
dedup_path_vars
if [[ -d "$modules/$version/$distro_dir/$distro_release" ]]; then
moduleenv="env LD_LIBRARY_PATH=$modules/$version/$distro_dir/$distro_release/lib"
modulecmd="$modules/$version/$distro_dir/$distro_release/bin/modulecmd"
else
moduleenv="env LD_LIBRARY_PATH=$modules/$version/$distro_dir/$distro_xrelease/lib"
modulecmd="$modules/$version/$distro_dir/$distro_xrelease/bin/modulecmd"
fi
if [[ ! -f "$modulecmd" ]]; then
# Fallback on system-installed
[[ $BITSENV_DEBUG == 1 ]] && printf "NOTICE: using modulecmd from the system\n" >&2
modulecmd=modulecmd
moduleenv=
fi
[[ $BITSENV_DEBUG == 1 ]] && printf "modulecmd=%s\nmoduleenv=%s\n" "$modulecmd" "$moduleenv" >&2
T=$(mktemp)
$moduleenv "$modulecmd" &> "$T"
if [[ $? == 127 ]]; then
printf "Unknown distribution release: %s %s\n" "$distro_name" "$distro_release"
[[ $BITSENV_DEBUG == 1 ]] && printf "ERROR: full error message is: %s\n" "$(cat "$T")" >&2
rm -f "$T"
exit 1
fi
rm -f "$T"
unset T
tclsh <<EOF >/dev/null 2>&1
EOF
[[ $? == 0 ]] && moduleenv=
command=""
# We cannot cross-pick pacakages among different platforms but we have to
# pick all packages consistently from a certain platform tree. When listing
# packages we show them all, when we load a package e define a priority list and
# we always have a fallback for backward compatibility.
ARGS=("$@")
PACKAGES=
EXPECT_PACKAGES=
for ARG in "$@"; do
if [[ $EXPECT_PACKAGES == 1 ]]; then
PACKAGES=$(normalize_sort_packages "$ARG")
break
elif [[ "$ARG" == enter || "$ARG" == printenv || "$ARG" == setenv || "$ARG" == checkenv ]]; then
EXPECT_PACKAGES=1
else
EXPECT_PACKAGES=
fi
done
export MODULEPATH="$moduledir/etc/toolchain/modulefiles/$platform:$moduledir/$platform/Modules/modulefiles"
[[ "$BITSENV_DEBUG" == 1 ]] && printf "MODULEPATH=%s\n" "$MODULEPATH" >&2
COMMAND_IN_ENV=()
while [ $# -gt 0 ]
do
case $1 in
enter)
shift 1
args=$(normalize_sort_packages "$1")
before=$(printenv)
Eval $moduleenv "$modulecmd" bash load $args 2>/dev/null || exit 1
dedup_path_vars
after=$(printenv | grep -v LS_COLORS=)
_LM_ENV=""
for var in $after
do
if [[ ! "$before" =~ "$var" ]]
then
_LM_ENV="$var;$_LM_ENV"
fi
done
export _LM_ENV
exec env PS1="[$args] \W > " bash --norc -i
;;
setenv)
shift 1
args=$(normalize_sort_packages "$1")
Eval $moduleenv "$modulecmd" bash load $args 2>/dev/null || exit 1
dedup_path_vars
shift 1
;;
checkenv)
shift 1
args=$(normalize_sort_packages "$1")
Eval $moduleenv "$modulecmd" bash load $args || exit 1
dedup_path_vars
PREV_PKG=
PREV_VER=
PKG_ERR=
while IFS= read -r LMF; do
VER=${LMF##*/}
PKG=${LMF%/*}
PKG=${PKG##*/}
if [[ "$PKG" == "$PREV_PKG" && "$VER" != "$PREV_VER" ]]; then
printf "ERROR: attempting to load %s %s when conflicting version %s already loaded\n" \
"$PKG" "$VER" "$PREV_VER" >&2
PKG_ERR=1
fi
PREV_PKG="$PKG"
PREV_VER="$VER"
done < <(printf '%s\n' "${_LMFILES_//:/$'\n'}" | sort)
[[ $PKG_ERR ]] && exit 1
[[ $BITSENV_DEBUG == 1 ]] && printf "NOTICE: all packages loaded successfully\n" >&2
exit 0
;;
printenv)
shift 1
if [[ -z "$1" ]]; then
echo "$_LM_ENV"
fi
args=$(normalize_sort_packages "$1")
$moduleenv "$modulecmd" bash load $args 2>/dev/null
exit
;;
-m|-?modules|-?mdir|-?moduledir)
shift 2
;;
-p|--platform)
shift 2
;;
-print)
shift 1
var=$1
echo ${!var}
shift 1
;;
-a|--archive)
shift 1
export MODULEPATH=$MODULEPATH:$(modulepath archive $moduledirs)
;;
-c)
shift; COMMAND_IN_ENV=("$@");
exec "${COMMAND_IN_ENV[@]}"
;;
-h)
Help
;;
q|query)
$moduleenv "$modulecmd" bash -t avail 2>&1 | grep -v -e : -e "^$"
exit $?
;;
*)
$moduleenv "$modulecmd" bash "$@"
exit
;;
esac
done