2019-09-05 19:22:32 +00:00
|
|
|
#!/usr/bin/env bash
|
2019-07-29 19:20:14 +00:00
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
# envs - manage user-submitted scripts and apps
|
2019-08-01 20:20:13 +00:00
|
|
|
# forked from tilde.team
|
2019-07-29 19:20:14 +00:00
|
|
|
|
2019-09-05 19:59:31 +00:00
|
|
|
# Copyright 2018-2019, Ben Harris <ben@tilde.team>
|
|
|
|
# Copyright 2019, Sven Kinne <creme@envs.net>
|
2019-07-29 19:20:14 +00:00
|
|
|
|
|
|
|
# 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]
|
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
2019-09-05 09:01:49 +00:00
|
|
|
export TERM=xterm-256color
|
2019-07-29 19:20:14 +00:00
|
|
|
|
|
|
|
PROGNAME=${0##*/}
|
2019-09-05 19:59:31 +00:00
|
|
|
VERSION="0.0.2"
|
2019-07-29 19:20:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
clean_up() { # Perform pre-exit housekeeping
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
error_exit() {
|
|
|
|
echo -e "${1:-"unknown Error"}" >&2
|
|
|
|
clean_up
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
graceful_exit() {
|
|
|
|
clean_up
|
|
|
|
exit
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
signal_exit() { # Handle trapped signals
|
|
|
|
case $1 in
|
|
|
|
INT)
|
|
|
|
error_exit "program interrupted by user" ;;
|
|
|
|
TERM)
|
2019-09-05 19:22:32 +00:00
|
|
|
printf '\n%s: program terminated\n' "$PROGNAME" >&2
|
2019-07-29 19:20:14 +00:00
|
|
|
graceful_exit ;;
|
|
|
|
*)
|
|
|
|
error_exit "$PROGNAME: terminating on unknown signal" ;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
prompt_confirm() {
|
|
|
|
while true; do
|
|
|
|
read -r -n 1 -p "${1:-continue?} [y/n]: " REPLY
|
2019-09-05 19:22:32 +00:00
|
|
|
case "$REPLY" in
|
2019-07-29 19:20:14 +00:00
|
|
|
[yY]) echo ; return 0 ;;
|
|
|
|
[nN]) echo ; return 1 ;;
|
2019-09-05 19:22:32 +00:00
|
|
|
*) printf ' \033[31m %s \n\033[0m' "invalid input"
|
2019-07-29 19:20:14 +00:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
usage() {
|
2019-09-05 19:22:32 +00:00
|
|
|
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"
|
2019-07-29 19:20:14 +00:00
|
|
|
[[ $(id -u) == 0 ]] && {
|
2019-09-05 19:22:32 +00:00
|
|
|
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"
|
2019-07-29 19:20:14 +00:00
|
|
|
}
|
2019-09-05 20:07:37 +00:00
|
|
|
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"
|
|
|
|
|
2019-07-29 19:20:14 +00:00
|
|
|
if [[ :$PATH: != *:"/envs/bin":* ]] ; then
|
2019-09-05 19:22:32 +00:00
|
|
|
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"
|
2019-07-29 19:20:14 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
help_message() {
|
2019-09-05 19:22:32 +00:00
|
|
|
cat <<- EOF
|
2019-09-05 19:59:31 +00:00
|
|
|
$PROGNAME (ver. $VERSION)
|
2019-07-29 19:20:14 +00:00
|
|
|
wrapper for user-submitted scripts
|
|
|
|
supports user submission and admin approval
|
|
|
|
$(usage)
|
2019-09-05 19:22:32 +00:00
|
|
|
EOF
|
2019-07-29 19:20:14 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
verify_script_name() {
|
|
|
|
[[ $1 == "" ]] && error_exit "please start over and enter the script name"
|
2019-09-05 19:22:32 +00:00
|
|
|
if [[ "$(type "$1" > /dev/null 2>&1)" ]]; then
|
2019-07-29 19:20:14 +00:00
|
|
|
error_exit "$1 already exists. rename your script and try again."
|
2019-07-29 19:43:00 +00:00
|
|
|
fi
|
2019-09-05 19:22:32 +00:00
|
|
|
[[ -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)
|
|
|
|
error_exit "$1 is a subcommand of envs. rename your script and try again.";;
|
2019-07-29 19:20:14 +00:00
|
|
|
*)
|
2019-09-05 19:22:32 +00:00
|
|
|
return;;
|
2019-07-29 19:20:14 +00:00
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
submission_checklist() {
|
2019-09-05 19:22:32 +00:00
|
|
|
cat <<- EOF
|
2019-07-29 19:20:14 +00:00
|
|
|
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
|
|
|
|
|
2019-09-05 19:22:32 +00:00
|
|
|
EOF
|
2019-07-29 19:20:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
mail_body() {
|
2019-09-05 19:22:32 +00:00
|
|
|
cat <<- EOF
|
2019-07-29 19:20:14 +00:00
|
|
|
Subject: envs script submission from $USER
|
|
|
|
From: $USER@envs.net
|
|
|
|
To: creme@envs.net
|
|
|
|
|
|
|
|
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:
|
2019-07-29 20:07:50 +00:00
|
|
|
sudo envs approve
|
2019-09-05 19:22:32 +00:00
|
|
|
EOF
|
2019-07-29 19:20:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# Trap signals
|
|
|
|
trap "signal_exit TERM" TERM HUP
|
|
|
|
trap "signal_exit INT" INT
|
|
|
|
|
|
|
|
|
|
|
|
# Parse command-line
|
2019-09-05 19:22:32 +00:00
|
|
|
case "$1" in
|
2019-07-29 19:20:14 +00:00
|
|
|
-h | --help | help)
|
|
|
|
help_message; graceful_exit ;;
|
|
|
|
|
|
|
|
-v | --version)
|
2019-09-05 19:22:32 +00:00
|
|
|
printf '%s\n' "$VERSION" ;;
|
2019-07-29 19:20:14 +00:00
|
|
|
|
2019-09-05 19:22:32 +00:00
|
|
|
'-*' | '--*')
|
2019-07-29 19:20:14 +00:00
|
|
|
usage
|
|
|
|
error_exit "Unknown option $1" ;;
|
|
|
|
|
|
|
|
list | ls)
|
2019-09-05 20:21:06 +00:00
|
|
|
printf 'available scripts:\n\n'
|
2019-07-29 19:33:53 +00:00
|
|
|
for scr in /envs/bin/*; do
|
2019-09-05 19:22:32 +00:00
|
|
|
script_name=$(basename "$scr")
|
2019-07-29 19:20:14 +00:00
|
|
|
target=$(readlink -f "$scr")
|
2019-09-05 19:22:32 +00:00
|
|
|
printf '%s%s by %s%s\n' "$(tput setaf 6)" "$script_name" "$(stat -c '%U' "$target")" "$(tput sgr0)"
|
|
|
|
cat /envs/descriptions/"$script_name"
|
|
|
|
printf '\n'
|
2019-07-29 19:20:14 +00:00
|
|
|
done ;;
|
|
|
|
|
2019-09-05 19:22:32 +00:00
|
|
|
about | apropos | description | desc)
|
|
|
|
if [[ -f /envs/descriptions/"$2" ]]; then
|
|
|
|
cat /envs/descriptions/"$2"
|
2019-07-29 19:20:14 +00:00
|
|
|
else
|
2019-09-05 19:22:32 +00:00
|
|
|
printf '%s not found. try %s list to see available user scripts.\n' "$2" "$PROGNAME"
|
2019-07-29 19:20:14 +00:00
|
|
|
fi
|
|
|
|
;;
|
|
|
|
|
|
|
|
submit)
|
2019-09-05 19:39:19 +00:00
|
|
|
printf "hello, %s! so it's time to submit your script?\n" "$USER"
|
2019-07-29 19:20:14 +00:00
|
|
|
submission_checklist
|
|
|
|
prompt_confirm "are you ready to continue?" || graceful_exit
|
2019-09-05 19:22:32 +00:00
|
|
|
printf 'enter the name of your script: '
|
|
|
|
read -r script_name
|
2019-07-29 19:20:14 +00:00
|
|
|
|
2019-09-05 19:22:32 +00:00
|
|
|
verify_script_name "$script_name"
|
2019-07-29 19:20:14 +00:00
|
|
|
|
2019-09-05 19:22:32 +00:00
|
|
|
if [[ -x "$HOME"/bin/"$script_name" ]]; then
|
2019-09-05 19:39:19 +00:00
|
|
|
printf '\ncool, found your script\n'
|
2019-09-05 19:22:32 +00:00
|
|
|
[[ -x /envs/pending-submissions/"$USER"/"$script_name"/"$script_name" ]] && error_exit "you've already submitted $script_name"
|
2019-07-29 19:20:14 +00:00
|
|
|
else
|
|
|
|
error_exit "$script_name not found in ~/bin"
|
|
|
|
fi
|
|
|
|
|
2019-09-05 19:22:32 +00:00
|
|
|
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'
|
2019-07-29 19:20:14 +00:00
|
|
|
prompt_confirm "ready to submit?" || graceful_exit
|
|
|
|
|
|
|
|
# submit now
|
2019-09-05 19:22:32 +00:00
|
|
|
mkdir -p /envs/pending-submissions/"$USER"/"$script_name"
|
|
|
|
ln -s "$HOME"/bin/"$script_name" /envs/pending-submissions/"$USER"/"$script_name"/"$script_name"
|
|
|
|
echo "$description" > /envs/pending-submissions/"$USER"/"$script_name"/description.txt
|
|
|
|
mail_body "$script_name" "$description" | /usr/sbin/sendmail creme
|
|
|
|
printf 'script submitted. thanks! :)\n' ;;
|
2019-07-29 19:20:14 +00:00
|
|
|
|
|
|
|
approve)
|
|
|
|
[[ $(id -u) != 0 ]] && error_exit "re-run this as sudo to access the approval queue"
|
|
|
|
|
2019-09-05 19:22:32 +00:00
|
|
|
printf 'welcome to the approval queue\n\n'
|
2019-07-29 19:20:14 +00:00
|
|
|
|
|
|
|
for user in /envs/pending-submissions/*; do
|
2019-09-05 19:22:32 +00:00
|
|
|
for scr in "$user"/*; do
|
|
|
|
user="$(basename "$user")"
|
|
|
|
script_name="$(basename "$scr")"
|
2019-07-29 19:20:14 +00:00
|
|
|
[[ -f $scr/approved ]] && continue
|
2019-09-05 19:22:32 +00:00
|
|
|
script="$scr"/"$script_name"
|
|
|
|
printf '%s by %s\n' "$script_name" "$user"
|
|
|
|
cat "$scr"/description.txt
|
2019-07-29 19:20:14 +00:00
|
|
|
prompt_confirm "approve?" || continue
|
|
|
|
|
2019-09-05 19:22:32 +00:00
|
|
|
sudo ln -s "$(readlink -f "$script")" /envs/bin/"$script_name"
|
|
|
|
sudo cp "$scr"/description.txt /envs/descriptions/"$script_name"
|
|
|
|
sudo touch "$scr"/approved
|
2019-07-29 19:20:14 +00:00
|
|
|
sudo chmod 664 /envs/descriptions/*
|
2019-09-05 19:22:32 +00:00
|
|
|
echo "your submission of $script_name has been approved and is now available at /envs/bin/$script_name" | /usr/sbin/sendmail "$user"
|
2019-07-29 19:20:14 +00:00
|
|
|
done
|
|
|
|
done
|
|
|
|
echo "~~done for now~~" ;;
|
|
|
|
|
|
|
|
revoke)
|
2019-09-05 19:22:32 +00:00
|
|
|
[[ "$(id -u)" != 0 ]] && error_exit "re-run this as sudo to access the revoke menu"
|
|
|
|
[[ -f /envs/bin/"$2" ]] || error_exit "$2 isn't an approved script"
|
2019-07-29 19:20:14 +00:00
|
|
|
|
|
|
|
prompt_confirm "revoke $2?"
|
2019-09-05 19:22:32 +00:00
|
|
|
printf 'please provide a reason: '
|
|
|
|
read -r reason
|
2019-07-29 19:20:14 +00:00
|
|
|
|
2019-09-05 19:22:32 +00:00
|
|
|
original_script="$(readlink -f /envs/bin/"$2")"
|
|
|
|
author="$(stat -c '%U' "$original_script")"
|
|
|
|
sudo rm /envs/{bin,descriptions}/"$2"
|
|
|
|
sudo rm -rf /envs/pending-submissions/"$author"/"$2"
|
2019-07-29 19:20:14 +00:00
|
|
|
|
2019-09-05 19:22:32 +00:00
|
|
|
echo -e "your script $2 has been returned because: $reason\nfeel free to resubmit" | /usr/sbin/sendmail "$author"
|
|
|
|
printf '%s revoked and returned to author\n' "$2" ;;
|
2019-07-29 19:20:14 +00:00
|
|
|
|
|
|
|
*)
|
2019-09-05 19:22:32 +00:00
|
|
|
if [[ -f /envs/bin/"$1" ]]; then
|
|
|
|
prog=/envs/bin/"$1"
|
2019-07-29 19:20:14 +00:00
|
|
|
shift
|
|
|
|
$prog "$@"
|
|
|
|
graceful_exit
|
|
|
|
else
|
2019-09-05 19:22:32 +00:00
|
|
|
[[ "$1" == "" ]] || printf "%s not found. try %s list to see what's available\n" "$1" "$PROGNAME"
|
2019-07-29 19:20:14 +00:00
|
|
|
help_message; graceful_exit;
|
|
|
|
fi ;;
|
|
|
|
|
|
|
|
esac
|
|
|
|
|
|
|
|
graceful_exit
|