From 436452cf7ae5965d34db3ab1c1df72e31e5f508e Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Sun, 19 Jan 2020 23:40:46 -0500 Subject: [PATCH] Add ability to run sudo commands from StumpWM --- stumpwm/.stumpwm.d/init.lisp | 13 +++ stumpwm/bin/stumpish | 196 +++++++++++++++++++++++++++++++++++ stumpwm/bin/stumpwm-sudo.sh | 2 + 3 files changed, 211 insertions(+) create mode 100755 stumpwm/bin/stumpish create mode 100755 stumpwm/bin/stumpwm-sudo.sh diff --git a/stumpwm/.stumpwm.d/init.lisp b/stumpwm/.stumpwm.d/init.lisp index ef3927a..8f03b9b 100644 --- a/stumpwm/.stumpwm.d/init.lisp +++ b/stumpwm/.stumpwm.d/init.lisp @@ -10,7 +10,20 @@ ;; StumpWM configs (setq *mouse-focus-policy* :click) +;; Sudo +(defcommand stumpwm-password (prompt) ((:string "prompt: ")) + "Prompts the user for a password" + (read-one-line (current-screen) prompt :password t)) + +(defmacro run-sudo-command (cmd) + `(run-shell-command + (format nil "SUDO_ASKPASS=~~/bin/stumpwm-sudo.sh sudo -A ~S" ,cmd) + t)) + ;; Commands +(defcommand shutdown () () + (run-sudo-command "shutdown now")) + (defcommand firefox () () (run-or-raise "firefox" '(:class "firefox"))) diff --git a/stumpwm/bin/stumpish b/stumpwm/bin/stumpish new file mode 100755 index 0000000..9e95c0c --- /dev/null +++ b/stumpwm/bin/stumpish @@ -0,0 +1,196 @@ +#!/bin/sh + +# Copyright (C) 2007 Jonathan Moore Liles +# +# Maintainer: Jonathan Moore Liles +# +# stumpish 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 2, or (at your option) +# any later version. +# +# stumpish 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 for more details. +# +# You should have received a copy of the GNU General Public License +# along with this software; see the file COPYING. If not, see +# . + +### STUMPwm Interactive SHell. + +DELAY=0.01 + +if ! sleep $DELAY 2>/dev/null >&2 +then + DELAY=1 +fi + +# replace -E with -r option for old versions of GNU sed +if ! sed -E 1p /dev/null 2>/dev/null; then + sed() { shift; command sed -r "$@"; } +fi + +# parse C-style backslash sequences by default +if [ "$(echo -e foo)" = foo ]; then + echo() { builtin echo -e "$@"; } +fi + +wait_result () +{ + while true + do + RESULT=$(xprop -root -f STUMPWM_COMMAND_RESULT 8s \ + STUMPWM_COMMAND_RESULT 2>/dev/null | + sed -E 's/\\([[:digit:]]+)/\\0\1/g') + if echo "$RESULT" | grep -v -q 'not found.$' + then + break + fi + + sleep $DELAY + done + + xprop -root -remove STUMPWM_COMMAND_RESULT + + if echo "$RESULT" | grep -q '= $' + then + return 0 + fi + + echo "$RESULT" | + sed -E 's/[^"\\n]+"// + /^"[[:space:]]*$/d + s/(^|[^\\])\\n/\1\ +/g + s/\\(["\\n])/\1/g + s/\^([*[:digit:]]+|[Bbn])//g' +} + +send_cmd () +{ + local cmd="$1" + + if [ "$cmd" = "stumpwm-quit" ] + then + cmd=quit + elif [ "$cmd" = "quit" ] + then + exit + fi + + xprop -root -f STUMPWM_COMMAND 8s -set STUMPWM_COMMAND "$cmd" + + wait_result +} + +usage () +{ + cat <&2 +} + +tput () +{ + local cap1=$1 cap2=$2 + shift 2 + + command tput $cap1 $@ 2>/dev/null || + command tput $cap2 $@ 2>/dev/null +} + +READLINE=yes + +if [ "x$1" = "x-r" ] +then + READLINE=no + shift 1 +fi + +if [ $# -gt 0 ] +then + [ "$1" = "--help" ] && usage + if [ "$1" = "-e" ] + then + if [ $# -ne 2 ] + then + echo "'-e' requires exactly one argument!" + exit + fi + shift 1 + IFS='' + ARGS=$(cat /dev/stdin) + send_cmd "$1 $ARGS" + else + IFS=' ' + send_cmd "$*" + fi +else + if [ -t 0 ] + then + if ! type rlwrap 2>/dev/null >&2 + then + warn rlwrap not found, command completion won\'t work + elif [ $READLINE = yes ] + then + COMMANDS="${TMPDIR:-/tmp}/stumpish.commands.$$" + echo $(send_cmd "commands") | + sed -E 's/[[:space:]]+/\ +/g' | + sort > "$COMMANDS" + trap 'rm -f "$COMMANDS"' exit int term + rlwrap -b '' -f "$COMMANDS" "$0" -r + exit + fi + + tput AF setaf 5 + echo Welcome to the STUMPwm Interactive SHell. + tput me sgr0 + echo 'Type \c' + tput AF setaf 2 + echo 'commands\c' + tput me sgr0 + echo \ for a list of commands. + + while read -p '> ' REPLY + do + tput md bold + tput AF setaf 2 + send_cmd "$REPLY" + tput me sgr0 + done + else + while read REPLY + do + send_cmd "$REPLY" + done + fi +fi diff --git a/stumpwm/bin/stumpwm-sudo.sh b/stumpwm/bin/stumpwm-sudo.sh new file mode 100755 index 0000000..746806f --- /dev/null +++ b/stumpwm/bin/stumpwm-sudo.sh @@ -0,0 +1,2 @@ +#!/usr/bin/env bash +stumpish stumpwm-password Password: