-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvs
executable file
·359 lines (290 loc) · 10.3 KB
/
envs
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
#!/usr/bin/env bash
# ---------------------------------------------------------------------------
# envs - manage users_info.json entrys and user-submitted scripts
# forked from tilde.team
# Copyright 2018, Ben Harris <[email protected]>
# Copyright 2019-2025, Sven Kinne <[email protected]>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License at <http://www.gnu.org/licenses/> for
# more details.
# Usage: envs [-h|--help]
# ---------------------------------------------------------------------------
export TERM=xterm-256color
PROGNAME=${0##*/}
VERSION="0.1.1"
user="$(whoami)"
hostname='envs.net'
INFO_FILE="$HOME/.envs"
###
# check coreutils and wrap stat for portability
if stat -c"%U" /dev/null >/dev/null 2>/dev/null ; then
# GNU environment
stat_func () { stat -c '%U' "$1"; }
else
# BSD environment
stat_func () { stat -f %Su "$1"; }
fi
isroot() {
[ "$(id -u)" = "0" ]
}
error_exit() {
printf '%s\n' "${1:-"unknown Error"}" >&2
exit 1
}
signal_exit() { # Handle trapped signals
case $1 in
INT)
error_exit "program interrupted by user"
;;
TERM)
printf '\n%s: program terminated\n' "$PROGNAME" >&2
exit
;;
*)
error_exit "$PROGNAME: terminating on unknown signal"
;;
esac
}
prompt_confirm() {
while true; do
printf "%s [y/n]: " "${1:-continue?}"
read -r REPLY
case $REPLY in
[yY]) printf '\n' ; return 0 ;;
[nN]) printf '\n' ; return 1 ;;
*) printf ' \033[31m invalid input \n\033[0m' ;;
esac
done
}
usage() {
printf '\n%sUser Scripts%s\n' "$(tput setaf 6)" "$(tput sgr0)"
printf '\nusage: %s [help|list|submit|about|script_name]\n' "$PROGNAME"
printf ' %s list - show a list of approved userscripts\n' "$PROGNAME"
printf ' %s submit - start the submission flow for your own script\n' "$PROGNAME"
if isroot; then
printf ' %s approve - enter the approval queue\n' "$PROGNAME"
printf ' %s revoke <script_name> - send a script back to the author and remove from /envs/bin\n' "$PROGNAME"
fi
printf ' %s about <script_name> - get the description for script_name\n' "$PROGNAME"
printf ' %s <script_name> - run script_name with all remaining args are passed to the script\n' "$PROGNAME"
printf '\n%sUser json-File Infomations%s\n' "$(tput setaf 6)" "$(tput sgr0)"
printf '\nusage: %s [show]|[edit]|[get name]|[set name value]|[unset name]\n' "$PROGNAME"
printf ' %s show - show your config file\n' "$PROGNAME"
printf ' %s edit - edit your config file\n' "$PROGNAME"
printf ' %s get <entry> - show a entry from your config\n' "$PROGNAME"
printf ' %s set <entry> <'\''value'\''> - set a entry to your config\n' "$PROGNAME"
printf ' %s unset <entry> - unset a entry from your config\n' "$PROGNAME"
if [[ :$PATH: != *:"/envs/bin":* ]] ; then
printf '\nadd /envs/bin to your PATH to use approved scripts without this wrapper\n'
printf 'if you'\''re using bash, run the following to add it quickly\n'
printf " echo 'export PATH=\$PATH:/envs/bin' >> ~/.bashrc && source ~/.bashrc\n"
fi
}
help_message() {
printf '%s%s (version %s)%s\n' "$(tput setaf 6)" "$PROGNAME" "$VERSION" "$(tput sgr0)"
printf 'wrapper for user-submitted scripts and users_info.json entrys\n'
printf 'supports user settings, submission and admin approval\n'
usage
}
verify_script_name() {
[[ -z $1 ]] && error_exit "please start over and enter a script name"
if command -v "$1"; then
if [ "$(command -v "$1")" != "/home/$user/bin/$1" ]; then
error_exit "$1 already exists. rename your script and try again."
fi
fi
[[ -x /envs/bin/"$1" ]] && error_exit "$1 is already taken. rename your script and try again."
case "$1" in
help|about|description|desc|list|ls|submit|apropos|approve|revoke|show|edit|get|set|unset)
error_exit "$1 is a subcommand of envs. rename your script and try again.";;
*)
return;;
esac
}
submission_checklist() {
cat <<- EOF
requirements for submitting a user script or program:
- placed in your ~/bin
- executable
- responds to help or --help
- no name collisions with existing scripts or $PROGNAME subcommands
EOF
}
mail_body() {
cat <<- EOF
Subject: envs script submission from ${user}
From: ${user}@${hostname}
To: creme@${hostname}
envs script submission from ${user}
script name: $1
description:
-----------------------------------------------------------------------
$2
-----------------------------------------------------------------------
you'll find the script and description in: /envs/pending-submissions/$user/$1
run this to see the approval queue:
sudo envs approve
EOF
}
#
# envs config
envs_show() { cat "$INFO_FILE" ; }
envs_edit() { if [[ -n "$EDITOR" ]]; then "$EDITOR" "$INFO_FILE"; else nano "INFO_FILE"; fi ; }
envs_get() { sed -n "/^$1=/{s#^.*=##;p}" "$INFO_FILE" ; }
envs_set() {
if [[ -z "$(sed -n /^"$1"=/p "$INFO_FILE")" ]]; then
echo "${1}=${2}" >> "$INFO_FILE"
else
sed -i "s#${1}=.*#${1}=${2}#" "$INFO_FILE"
fi
}
envs_unset() { sed -i "s#${1}=.*#${1}=#" "$INFO_FILE" ; }
# add default config if not exists
if [ ! -f "$INFO_FILE" ]; then
cat << EOF > "$INFO_FILE"
#
# ENVS.NET - user information config
# ( will be updated every hour )
# here you can add more details to youre users_info.json part
#
desc=a short describtion or message
# to print your ssh-pubkey in users_info.json enable this option
# set to 'y' / 'Y' or '1'
ssh_pubkey=0
# add more informations (max. 10 entrys)
#git=
#mastodon=
# you can also add a array. (each with 32 lines limit)
#name=line1
#name=line2
#nametwo=line1
EOF
fi
# check default config options
[ "$(envs_get desc)" == '' ] && envs_set desc 'a short describtion or message'
[ "$(envs_get ssh_pubkey)" == '' ] && envs_set ssh_pubkey 0
# Trap signals
trap "signal_exit TERM" TERM HUP
trap "signal_exit INT" INT
# Parse command-line
case "$1" in
-h | --help | help)
help_message; exit
;;
-v | --version)
printf '%s\n' "$VERSION"
;;
'-*' | '--*')
usage
error_exit "Unknown option $1"
;;
list | ls)
printf 'available scripts:\n\n'
for scr in /envs/bin/*; do
if [ -f "$scr" ]; then
script_name=$(basename "$scr")
target=$(readlink -f "$scr")
printf '%s%s by %s%s\n' "$(tput setaf 6)" "$script_name" "$(stat_func "$target")" "$(tput sgr0)"
cat /envs/descriptions/"$script_name"
printf '\n'
fi
done
;;
about | apropos | description | desc)
if [[ -f /envs/descriptions/"$2" ]]; then
cat /envs/descriptions/"$2"
else
printf '%s not found. try %s list to see available user scripts.\n' "$2" "$PROGNAME"
fi
;;
submit)
printf 'hello, %s! so it'\'s' time to submit your script?\n' "$user"
submission_checklist
prompt_confirm "are you ready to continue?" || exit
printf 'enter the name of your script: '
read -r script_name
verify_script_name "$script_name"
if [[ -x "$HOME"/bin/"$script_name" ]]; then
printf '\ncool, found your script\n'
[[ -x /envs/pending-submissions/"$user"/"$script_name"/"$script_name" ]] && error_exit "you've already submitted $script_name"
else
error_exit "$script_name not found in ~/bin"
fi
printf 'enter a description of your script:\n'
read -r description
printf '\nyour script, along with your description will be sent to the admins for approval\n'
prompt_confirm "ready to submit?" || exit
# submit now
mkdir -p /envs/pending-submissions/"$user"/"$script_name"
ln -s "$HOME"/bin/"$script_name" /envs/pending-submissions/"$user"/"$script_name"/"$script_name"
printf '%s\n' "$description" > /envs/pending-submissions/"$user"/"$script_name"/description.txt
mail_body "$script_name" "$description" | /usr/sbin/sendmail creme
printf 'script submitted. thanks! :)\n'
;;
approve)
if ! isroot; then
error_exit "re-run this as sudo to access the approval queue"
fi
printf 'welcome to the approval queue\n\n'
for user in /envs/pending-submissions/*; do
for scr in "$user"/*; do
user="$(basename "$user")"
script_name="$(basename "$scr")"
[[ -f $scr/approved ]] && continue
script="$scr"/"$script_name"
if [ -f "$script" ]; then
printf '%s by %s\n' "$script_name" "$user"
cat "$scr"/description.txt
prompt_confirm "approve?" || continue
ln -s "$(readlink -f "$script")" /envs/bin/"$script_name"
cp "$scr"/description.txt /envs/descriptions/"$script_name"
touch "$scr"/approved
chmod -R 664 /envs/descriptions ; chmod 755 /envs/descriptions
chown -R root:envs /envs/descriptions /envs/bin
printf 'your submission of %s has been approved and is now available at /envs/bin/%s' "$script_name" "$script_name" | /usr/sbin/sendmail "$user"
fi
done
done
printf '~~done for now~~\n'
;;
revoke)
isroot || error_exit "re-run this as sudo to access the revoke menu"
[ -f /envs/bin/"$2" ] || error_exit "$2 isn't an approved script"
prompt_confirm "revoke $2?"
printf 'please provide a reason: '
read -r reason
original_script="$(readlink -f /envs/bin/"$2")"
author="$(stat_func "$original_script")"
rm /envs/{bin,descriptions}/"$2"
rm -rf /envs/pending-submissions/"$author"/"$2"
printf 'your script %s has been returned because: %s\nfeel free to resubmit\n' "$2" "$reason" | /usr/sbin/sendmail "$author"
printf '%s revoked and returned to author\n' "$2"
;;
show) envs_show ;;
edit) envs_edit ;;
get) envs_get "$2" ;;
set) envs_set "$2" "$3" ;;
unset) envs_unset "$2" ;;
*)
if [ -z "$1" ]; then
help_message
exit
elif [ -x /envs/bin/"$1" ]; then
prog="/envs/bin/$1"
shift
exec $prog "$@"
else
printf '%s not found. check %s list to see what'\'s' available\n' "$1" "$PROGNAME"
help_message
exit
fi
;;
esac
#
exit