#!/bin/sh # ------- # File: pe # Description: very simple script to run a program as root # using op, gksu or sudo (in this order) # Author: Luis Garcia Gisbert # # 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 2 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 for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin St, Fifth Floor, Boston MA 02110-1301 USA # -------- set -e PATH="/usr/sbin:/usr/bin:/sbin:/bin" # library LLX_COMMONLIB="/usr/share/lliurex/llxcfg-common/llxcfg-common.sh" [ -f "${LLX_COMMONLIB}" ] || exit 1 . ${LLX_COMMONLIB} # functions usage() { CMD_NAME="$(basename "$0")" echo "Usage: $CMD_NAME [options] COMMAND [ ARGUMENTS ...]" >&2 echo " options: --no-interactive\n\ --test\n\ --list\n\ --update" >&2 } op_file(){ BASE_CMD="$(basename "$1")" echo "$OP_PATH/pe-$BASE_CMD.conf" } pe_default_template(){ llxcfg-config read "$PE_CONF_DEFAULT" return 0 } pe_update(){ # write new files for PE_CMD in $(find "$PE_CFG_PATH" -maxdepth 1 -mindepth 1 -xtype d -printf "%P\n") ; do CMD_FILE="$(find "$PE_CFG_PATH/$PE_CMD" -xtype f -name "*.${PE_EXT_CMD}" |head -1)" if [ -r "$CMD_FILE" ] ; then PE_FULLCMD="$(cat $CMD_FILE)" if [ "$PE_FULLCMD" ] ; then PE_USERS="$(find "$PE_CFG_PATH/$PE_CMD" "$PE_ADMIN_PATH" -xtype f -name "*.${PE_EXT_USERS}" -exec cat {} \; |tr "\n" "," |sort -u)" PE_USERS="${PE_USERS%,}" PE_GROUPS="$(find "$PE_CFG_PATH/$PE_CMD" "$PE_ADMIN_PATH" -xtype f -name "*.${PE_EXT_GROUPS}" -exec cat {} \; |tr "\n" "," |sort -u)" PE_GROUPS="${PE_GROUPS%,}" TMP_FILE="$(tempfile)" llxcfg-config read "$PE_CONF/$PE_CMD" || pe_default_template > "$TMP_FILE" export PE_CMD PE_FULLCMD PE_USERS PE_GROUPS OP_FILE="$(op_file "$PE_FULLCMD")" llxcfg-template "$TMP_FILE" |llxcfg-install --mode=600 - "$OP_FILE" rm -f "$TMP_FILE" unset PE_CMD PE_FULLCMD PE_USERS PE_GROUPS fi fi done # remove old files for PE_FILE in $(find "$OP_PATH" -maxdepth 1 -mindepth 1 -xtype f -name "pe-*.conf") ; do PE_CMD="$(basename "$PE_FILE" ".conf")" PE_CMD="${PE_CMD#pe-}" [ -d "$PE_CFG_PATH/$PE_CMD" ] || rm -f "$PE_FILE" done return 0 } pe_purge(){ find "$OP_PATH" -maxdepth 1 -mindepth 1 -xtype f -name "pe-*.conf" --exec rm {} \; } op_grep(){ if op -l |grep -q "^${1}\b"; then echo "${1}" return 0 fi # try in the help (assume help is just the full path of command) CMD="$(op -l |sed -ne "\%[[:blank:]]${1}[[:blank:]]*$%{s%[[:blank:]].*$%%;p}")" if [ "$CMD" ] ; then echo "$CMD" return 0 fi return 1 } op_test(){ return 0 # 1. try command "as is" ! op_grep "$1" || return 0 # 2. try basename of command CMD="$(basename "$1")" ! op_grep "$CMD" || return 0 # 3. Last chance ... may be the command is just a link of other command, so try basename of readlink -f CMD="$(readlink -f "$1")" ! op_grep "$(basename "$CMD")" || return 0 return 1 } # ---- # main # ---- NO_INTERACTIVE="" # check option(s) case "$1" in --no-interactive) NO_INTERACTIVE="Y" shift ;; --test) shift if [ -z "$1" ] ; then usage exit 1 fi ! op_test "$1" >/dev/null || exit 0 exit 1 ;; --list) rc=0 for zmd in $(ls /etc/op/op.d/ -1 | sed "s%\.conf$%%g" | sed "s%^pe\-%%g"); do echo "$zmd /usr/share/zero-center/zmds/$zmd" done #op -l || rc =$? exit $rc ;; --update) rc=0 pe_update || rc =$? exit $rc ;; --*) usage exit 1 ;; esac if [ -z "$1" ] ; then usage exit 1 fi CMD="$1" shift # 1. am I root? [ $(id -u) -ne 0 ] || exec "$CMD" "$@" # 2. can I use op? #if OP_CMD="$(op_test "$CMD")" ; then exec op "$CMD" "$@" #fi [ -z "$NO_INTERACTIVE" ] || exit 1 # 3. running under graphical environment and gksu is available? [ -z "$DISPLAY" ] || [ ! -x /usr/bin/gksu ] || exec gksu "$CMD" "$@" # 4. sudo is our last chance to escale privileges exec sudo "$CMD" "$@"